Forum Moderators: coopster

Message Too Old, No Replies

Regex replace

         

jojy

10:17 am on Jan 12, 2009 (gmt 0)

10+ Year Member



Hello,
I am trying to replace html elements with new line but it doesn't work. I am not good at regular expression. Here is the regex code

$patterns = array('/\<p\>(.*)\<\/p\>/i','/\<br\/\>/i','/\<br \/\>/i','/\<br\>/');
$replace = array("$1\n","\n");
echo preg_replace($patterns,$replace,$str);

Any help would be much appreciated..

Thanks

wildbest

10:59 am on Jan 12, 2009 (gmt 0)

10+ Year Member



If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string.

jojy

5:29 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



sorry forgot to add last parameter .. here is code again
$patterns = array('/\<p\>(.*)\<\/p\>/i','/\<br\/\>/i','/\<br \/\>/i','/\<br\>/i');
$replace = array("$1\n","\n","\n");
echo preg_replace($patterns,$replace,$str);

it works with one line html but not with multi line html
i.e: <p>I am inside html tag</p>I am just string <br/>Another string

wildbest

5:40 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



1. Your patterns array has 4 elements while your replace has only 3. Please, read carefully what I've said in my previous post.

2. The 3rd element in your patterns array has whitespace which should be escaped like this \s and should be '/\<br\s\/\>/i'

3. To my knowledge there is no need to escape the "<" and ">" signs but if you escape them in current situation should not be a problem.

4. you might wish to try this format instead:
<p\b[^>]*>(.*?)</p>

jojy

5:58 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



Thanks, wildbest. I figured out why its not replacing tags. (.*) this is for all characters and no line breaks while this one (.*?) includes line breaks.

Thanks for your help

wildbest

6:39 pm on Jan 12, 2009 (gmt 0)

10+ Year Member



No, actually (.*) is a greedy match and the question mark in the regex makes the star lazy to make sure it stops before the first closing tag.

If you want the dot in (.*) to include line breaks you'll have to add "s" to the regex flags after the second delimiter. If you deal with Unicode strings you'll have to include "U" flag as well. I'm not sure if you can use the global flag "g" in PHP preg_replace pattern string to create array of all occurrences and replace them with their replacement array counterpart string.

PS: In fact "u" is for Unicode and "U" is for Ungreedy (.*) and this is the official list of supported preg_replace regex modes:
[us3.php.net...]