开发者

preg_match help

开发者 https://www.devze.com 2023-01-10 05:49 出处:网络
I\'m using preg_match for the first time but I\'m stuck even before I got so开发者_如何学Cme code...

I'm using preg_match for the first time but I'm stuck even before I got so开发者_如何学Cme code...

With the help from RegExr I have figured out that I need to use this expression:

/\(.*M\)\s.*?:/gm

What I need help with is how I gonna use this to place<b></b>around the matched text.

Gratefull for help.


$output = preg_replace('%\(.*M\)\s.*?:%', '<b>$0</b>', $input);

If you're working in PHP, you'd be better off using this online tester:

http://www.spaweditor.com/scripts/regex/

RegExr is a Flex app; the regex engine is the same, but the code used to invoke it is very different. For example (as Matthew pointed out), there's no g flag; you would use the preg_match_all method instead. Remember to add regex delimiters (I used %); I'm always forgetting those.


preg_match('/\(.*M\)\s.*?:/', $input, $matches);
$output = "<b>$matches[0]</b>";

There is no g flag in PCRE, and I don't think you need multiline, because you're not using ^ or $.

0

精彩评论

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