Forum Moderators: coopster

Message Too Old, No Replies

regular expression puzzle

         

daktau

1:14 pm on Jun 9, 2010 (gmt 0)

10+ Year Member



Hi,
Sorry for being so slow at regular expressions but I am having real difficulty getting anything to work.

I need to scan some content and replace anything followed by a ":<br>" to have some styling attached to it.

So far my regular expression is as follows...

echo(preg_replace('(.*):<br>','<div class="title">$1:</div>',$v['content']));

Howerver, I keep receiveing this error message

preg_replace() [function.preg-replace]: Unknown modifier ':'

Can anyone point me in the right direction please?

cheers,
George

Alcoholico

3:18 pm on Jun 9, 2010 (gmt 0)

10+ Year Member



This is how I'd attempt to do it, I have not tested it though:
$search = '/([^:]*):<br>/iUs';
$replacement = '<div class="title">$1:</div>';
$result = preg_replace($search, $replacement, $v['content']);

rocknbil

4:12 pm on Jun 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo(preg_replace('(.*):<br>','<div class="title">$1:</div>',$v['content']));
Howerver, I keep receiveing this error message
preg_replace() [function.preg-replace]: Unknown modifier ':'


You need regex delimiters around the pattern. The single quotes ' are the PHP delimiters defining the pattern, so the parser is seeing () as delimiters, interpreting : as a modifier, and there's no telling what it would do with <br>, it just stopped there. A modifier is a flag to tell the regex engine how to treat the match, example, i means treat it as case-insensitive.

For whatever reason, many PHP coders use # as a delimiter, I use the old school slash.

$new_content = preg_replace('/(.*):<br>/i','<div class="title">$1:</div>',$v['content']));

Note the i modifier, it will work with <br> or <BR>. But I still don't think it will do what you want.

. is any character, .* is zero or more of any character which may slurp up any <br>'s it finds until it finds the last one. This is called "greedy pattern matching." So you need some form of quantifier, through a ? or a U modifier, to insure it matches and replaces each instance. Second, if this crosses multiple lines, you may need an s modifier. So my first stab at it would be

$new_content = preg_replace('/(.*?):<br>/is',"<div class=\"title\">$1:</div>",$v['content']));

or

$new_content = preg_replace('/(.*):<br>/iUs',"<div class=\"title\">$1:</div>",$v['content']));

Note the difference is the ? directly in the regex in the first, and the U modifier in the second, these should be more or less equivalent.

Caveat: I changed the single quotes to double quotes, as I'm *pretty sure* $1 is still a variable and may not interpolate in single quotes. Could be wrong, if so, you can leave them as singles.

Alcoholico's attempt is much more graceful, but would *possibly* fail on

So hear is the deal: how do I grok regexps?:<br>

But it's DEFINITELY a good idea to variable-ize your work. Combination of the two,

$search = '/(.*):<br>/iUs';
$replacement = "<div class=\"title\">$1:</div>";
$new_content = preg_replace($search, $replacement, $v['content']);

(You sure you don't want a p there, or an h# if it's a real title, instead of generic div, for more semantic output? :-)