tags:

views:

47

answers:

2

I want to know if a string is in another string at least once.

I want to check if Shadowbox.init then any (optionnal) combisaison of whitespace then ( is inside $myString at least once.

  • Shadowbox.init must be found (case sensitive)
  • It can be followed by any number of white spaces (spaces, tabs, newlines...)
  • The whitespaces or init is then followed by an opening parenthesis (

So far I have:

$matches = preg_match('/Shadowbox.init[\s]*(/', $myString);

Some examples:

/*
Shadowbox.init(); --> OK
Shadowbox.init ( ); --> OK
Shadowbox.init       (foo); --> OK
Shadowbox.init ); --> Bad missing (
Shadowbox.init --> Bad missing (
Shadowbox. init(); --> Bad space after dot
*/
A: 

You don't need regex: http://php.net/manual/en/function.strpos.php

CrociDB
"Shadowbox.init" and "(" can be separated by ANY NUMBER/COMBINAISON of whitespaces.
Activist
There was a little more to the problem than that. :)
André Laszlo
+3  A: 

You were nearly there:

$matches = preg_match('/Shadowbox\.init\s*\(/', $myString);

You need to escape the parenthesis and the dot. And you don't need to put brackets around \s.

Tim Pietzcker
Slowly learning... OK so 1) you escaped the dot with a backslash 2) escaped the ( with a backslash 3) removed [] between \s. Why 3 is needed? Can you explain?
Activist
The brackets are just not necessary - you need them only if you have more than one item inside the character class you're defining, like `[abc]`. Since `\s` is a single token, you can drop the brackets, but they don't hurt either.
Tim Pietzcker