开发者

How do I match bb style syntax with regular expressions?

This is my regular expression:

/^\[y\](.*?)\[\/y\]/

This is my subject:

youtube is here [y]2GJSVlIGmQI[/y]

but preg_match() doesn't match anything from my subject开发者_运维百科.

And also is there a better solution to capture the code inside the [y] [/y]'s?

Thanks!


You have to remove ^ char from your regex. ^ char matches the start of a string.

$str = 'youtube is here [y]2GJSVlIGmQI[/y]';

preg_match('/\[y\](.*?)\[\/y\]/', $str, $arr);

// 2GJSVlIGmQI
echo $arr[1];


You need to remove the ^ at the beginning, like this:

/\[y\](.*?)\[\/y\]/

What the ^ does is match that expression only if it is at the start of the string. Removing it means the expression will match the pattern anywhere in the string.


Use this: /\[y\](.*?)\[\/y\]/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜