Re: regex help needed

From: Date: Sun, 01 Aug 2004 18:52:06 +0000
Subject: Re: regex help needed
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-192442@lists.php.net to get a copy of this message
hi M. Sokolewicz a écrit :
You could try something like: $return = preg_replace('#<h[1-9]>(.*)</h[1-9]>#Uie', 'str_replace("<br />", "", "$1")'); - Tul Kathleen Ballard wrote:
Sorry, Here is the code I am using to match the <h*> tags: <h([1-9]){1}>.*</h([1-9]){1}> I think this mask is better :
<h([1-6])>.*?</h\1)>sie
I have removed all the NL and CR chars from the string I am matching to make things easier. Also, I have run tidy on the code so the tags are all uniform. The above string seems to match the tag well now, but I still need to remove the br tags from the tag contents (.*). To remove the <br /> tags, you need to call preg_replace_callback() :
<?php $str = '<h1>hi <br /> ..</h1> bla bla <h5> .... <br /> ..</h5> ...<br />'; function cbk_br($match) { return '<h' . $match[1] . '>' . str_replace('<br />', '', $match[2]) . '</h' . $match[1] . '>'; } $return = preg_replace_callback('<h([1-6])>(.*?)</h\1>si', 'cbk_br', $str); echo $return; ?>
The strings I will be matching are html formatted text. Sample <h*> tags with content are below: <h4>Ex-Secretary Mickey Mouse <br />Loses Mass. Primary</h4> <h4>Ex-Secretary Mickey Mouse <br />Loses Mass. Primary <br /> Wins New Jersey</h4> <h4>Ex-Secretary Reich Loses Mass. Primary</h4> Again, any help is appreciated. Kathleen
-- Fabrice Lezoray http://classes.scriptsphp.fr -----------------------------

« previous php.general (#192442) next »