开发者

Regular expression to match valid calls to Shadowbox.init()

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开发者_JAVA技巧 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
*/


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜