0

I just want to stress that I've been reading, trying and failing a lot before I asked this question. I can see similar questions but not exact duplicates.

I need to find a value that:

  • Could be in any case.
  • Could or could not end with something completely random E.g. "Cake is yummy"
  • May or may not be enclosed in /slashes/

This is what I'm currently doing:

  if ( $name == '/cake/' || $name == '/CAKE/' || $name == '/Cake/' || $name == '/CaKe/' || $name == 'CAKE' || $name == 'cake' { 

This is what I'm trying to do:

if(preg_match == (cake:) *(\([a-zA-Z]+\).+){

Can't quite get it to work.

10
  • Do you have regexp enclosed in quotes? Commented Aug 8, 2015 at 11:08
  • Wouldn't it be easier to make the string uppercase first and then do $name = strtoupper($name); if($name == '/CAKE/' || $name == 'CAKE') {? Commented Aug 8, 2015 at 11:09
  • I think you need to be much more detailed in what you are trying to match. Is it just single words? What do you mean it may end with random stuff? Should that be matched as well? Etc. When working with regular expressions you need to to very specific in what you want and what you do not want. Commented Aug 8, 2015 at 11:10
  • Still doesn't account for random strings after Commented Aug 8, 2015 at 11:10
  • @Cyclone If it can be in any case then a case-insensitive match would do just fine. There is no reason to uppercase it. Commented Aug 8, 2015 at 11:11

1 Answer 1

1

You're basically searching for a case-insensitive Cake. Then just use the i flag.

  preg_match("~ Cake ~xi", $name)
                       ↑

If you also want to match for optional / slashes around, then add an altenative:

  preg_match("~ /Cake/  |  \b Cake \b ~xi", $name)
                ↑    ↑      ↑       ↑

Note the \b word boundary markers for the unenclosed cake, so you're not matching Cakeriky for example.

  1. If you want Cake to be at the start of the string, then it'll need an ^ marker.

     preg_match("~ ^  (/Cake/  |  \b Cake \b) ~xi", $name)
                   ↑
              start anchor
    
  2. If you additionally wanted to enforce some text thereafter being present (and not optional/ignored), then add another placeholder:

     preg_match("~ ^  (/Cake/  |  \b Cake \b)   .*\w.* ~xi", $name)
                                                   ↑
                                          at least one word char
    
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, how about if I wanted to match '/cake/ - bake some!' for example, it could be anything afterwards
so for example: if $name preg_match("~ /Cake/ | \b Cake \b ~xi",){ Would find cake, /cake/, cake is yummy, /cake/ is great etc. ?
Any combination of that. Add the $name parameter to match against.
Thanks for all your help can you just check my syntax here I'm unsure how to incorporate it Something like: if ($name == (preg_match("~ /Cake/ | \b Cake \b ~xi",)){ Or if(preg_match("~ /Cake/ | \b Cake \b ~xi", $name))
Thank you so much for all your help and patience :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.