Re: Help with regular expresions! [ASAP]

From: Date: Wed, 25 Jul 2001 13:53:21 +0000
Subject: Re: Help with regular expresions! [ASAP]
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-59541@lists.php.net to get a copy of this message
Hi Peter, You could try this: <? $string = "Blah <i>italic</i><input type=\"name\"> Blah <b>bold</b>"; $new_string = preg_replace("/<input(.*?)>/i", "<input\\1 class=3D>", $string); echo "$string<br><br>$new_string"; ?> preg_replace = regular expression replacement <input = first part of tag (.*?) = whatever, but not: > .* is greedy... Match any character . as many times * as necessary, made less greedy because the ? looks for whatever's to the right of it.... If you did: preg_replace("/<input(.*)>/i", "<input\\1 class=3D>", $string); it might replace <input type="text" name="blah"><input type="file"> with <input type="text" name="blah"><input type="file" class=3D> Instead of <input type="text" name="blah" class=3D><input type="file" class=3D> Which is what you want. The \\1 is what you have captured from the first (and in this case, only) set of parentheses () The i after the delimiter means case insensitive. Note, I didn't have time to check that this works, but you get the principle :) James "Psychosphere2k" <PsychoSphere2K@hotmail.com> wrote in message news:OE584ZneAzdfTVSxzW20000318c@hotmail.com... Hi! I have a fairly large PHP based website. Unfortuantely, I didn't make extensive use of stylesheets. I'm hoping for some help with regular expressions matching ( and = replacing ) some tags. I use output buffering, so it would be a simple case of adding some = lines to my output hanlder. eg: replacing < input .... > with < input .... class =3D "something " > and < body .... > with < body .... class =3D "something_else" > Some code, with comments would be really great. Thanks in advance. Peter Gibson

« previous php.general (#59541) next »