preg_match question

If I have a string:

<strong>Name:</strong> Johnny Bravo

How might I match “Johnny Bravo” with a preg_match? I’ve tried so many variations my head is spinning…

Thanks in advanced.

preg_match("/<strong>Name:</strong>(.*)/", $str, $matches);

That’s from what I can see from your string, if it is that simple - this regexp should do, but you can also use str_* functions to achieve the same result.

I seem to be getting this error:

Message: preg_match_all() [function.preg-match-all]: Unknown modifier 't'

Any ideas?

Try posting the code that produced the error, as I haven’t used a modifier in the example I posted. Guessing around without the code won’t get us anywhere :slight_smile:


		$content = '<strong>Name:</strong> Hello';
		preg_match_all("/<strong>Name:</strong>(.*)/", $content, $matches);
		print_r($matches);

My fault there, try with

preg_match_all("#<strong>Name:</strong>(.*)#", $content, $matches);

Thanks that did the trick.

What makes a difference with the # signs versus / 's?

The forward slash (/) indicates the start and end of your pattern, as you use a forward slash in your pattern (</strong>) it breaks.

Using the octothorpe symbol (#, hash), it avoids this.

:slight_smile: