views:

39

answers:

2

Hi!

I'm using preg_match for the first time but I'm stuck even before I got some 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.

+1  A: 
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 $.

Matthew Flaschen
I don't think that code outputs <b>(8:00:36 PM) diggan:</b> cause I can't find any <b> in your code...
Victor
@Victor, this is the first time you said anything about `<b>`. Modify your question and state what the input and output should be.
Matthew Flaschen
@Matthew Flaschen, in my post there is <b> and </b> but they arent visible... I don't know why...
Victor
+1  A: 
$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.

Alan Moore
Thank you for your help!
Victor