RE: [PHP] Regex Help

From: Date: Thu, 19 Dec 2002 18:53:46 +0000
Subject: RE: [PHP] Regex Help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-128901@lists.php.net to get a copy of this message
There may be better ways, but this little example works. You basically match everything around the <option> tag and run a replace on what's outside of it, and then put it all back together. This example should run as is. Adapt to your needs. <? header("Content-type: text/plain"); $word = "test"; $repl = "anotherword"; $new_string = ""; $string = "This test is a test okay? <option value='test'>test</option> and this test is good This test on line 2<option value='whatever'>thing</option> <option value='foo'>foo</option> test on line 3 <option value='foo2'>foo2</option> Test<option>test</option>Test"; preg_match_all("/(.*)(<option.*option>)(.*)/i",$string,$match); $cnt = count($match[1]); for($x=0;$x<$cnt;$x++) { $p1 = preg_replace("/$word/i",$repl,$match[1][$x]); $p2 = preg_replace("/$word/i",$repl,$match[3][$x]); $new_string .= $p1 . $match[2][$x] . $p2 . "\n"; } echo "$string\n--\n$new_string"; ?> ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ > -----Original Message----- > From: Jim [mailto:user@lifeforjesus.org] > Sent: Thursday, December 19, 2002 1:24 PM > To: php-general@lists.php.net > Subject: Re: [PHP] Regex Help > > Thanks for helping. Unfortunately, that doesn't quite accomplish the task > either. The other example for my first post would be mangled with that. > > ----- Original Message ----- > From: "Rick Emery" <remery@emeryloftus.com> > To: "Jim" <user@lifeforjesus.org>; <php-general@lists.php.net> > Sent: Thursday, December 19, 2002 1:09 PM > Subject: Re: [PHP] Regex Help > > > > <?php > > $q = "test <OPTION VALUE=\"test\">test</OPTION> test"; > > ereg("(.*)(<OPTION.*OPTION>)(.*)",$q,$ar); > > $t = "anotherword".$ar[2]."anotherword"; > > print $t; > > ?> > > > > outputs: > > anotherword<OPTION VALUE="test">test</OPTION>anotherword > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#128901) next »