Re: regex help needed

From: Date: Mon, 02 Aug 2004 03:14:46 +0000
Subject: Re: regex help needed
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-192456@lists.php.net to get a copy of this message
On Sun, 1 Aug 2004 10:38:06 -0700 (PDT) Kathleen Ballard <klb5770@yahoo.com> wrote: > Sorry, > Here is the code I am using to match the <h*> tags: > > <h([1-9]){1}>.*</h([1-9]){1}> > > 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 (.*). > > 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 Simple: while (preg_match("/(<h\d>)(.*)(<br \/>)(.*)(<\/h\d>)/is", $str)) { $str = preg_replace("/(<h\d>)(.*)(<br \/>)(.*)(<\/h\d>)/is", "$1$2$4$5", $str); } $str = preg_replace("/(<h\d>)(.*)(<\/h\d>)/is", "$2", $str); Recommended: $str = preg_replace("/(<h\d)([^>]*)(>)(.*)(<\/h\d>)/eis", "remove_br('$4')", $str); function remove_br($str){ return preg_replace("/(<br)([^>]*)(>)/i", "", $str); }

« previous php.general (#192456) next »