Re: PHP 4.0 Bug #3850: ereg_replace broken when pattern string is too long

From: Date: Thu, 16 Mar 2000 22:26:26 +0000
Subject: Re: PHP 4.0 Bug #3850: ereg_replace broken when pattern string is too long
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-16914@lists.php.net to get a copy of this message
brian@phorum.org writes: > From: brian@phorum.org > Operating system: Solaris and Windows > PHP version: 4.0 Beta 2 > PHP Bug Type: Misbehaving function > Bug description: ereg_replace broken when pattern string is too long > > This script: > ------------------------------ > $body='<table>'; > $body=ereg_replace("<(/*[b|u|i|font|ol|ul|li|img|a] *[^>]*)>", > "[\\1]", $body); > echo $body; > > produces: > ------------------------------ > [table] > > That is wrong. No, that is correct. I think you're misinterpreting what the [b|u|i|font|ol|ul|li|img|a] is doing. It means 'match any of the characters b, u, i, f, o, n, t, m, g, a, or |', when what I think you want is (b|u|i|font|ol|ul|li|img|a), which means 'match any of the strings b, u, i, font, ol, ul, li, img, or a'. You are asking the regexp to match: o An opening angle bracket, followed by o An atom consisting of: o zero or more slashes, followed by o one of the characters 'buifontmga|', followed by o zero or more spaces, followed by o zero or more of anything which isn't a closing angle bracket o ...and a closing angle bracket. > Shorten the pattern: > ------------------------------ > $body='<table>'; > $body=ereg_replace("<(/*[b|u|i] *[^>]*)>", "[\\1]", $body); > echo $body; > > That produces: > ------------------------------ > <table> > > AS it should. This appears to be a problem in 3.x as well. The reason the second one works is not the length of the regexp, but rather the fact that the [b|u|i] won't match the first character of 'table', whereas [b|u|i|font|ol|ul|li|img|a] will. Here's a modified version and a quick test situation which produces what I think you're trying to achieve; ---8<---test.php---8<--- #!/usr/local/bin/php -q <?php $filename = 'http://www.thebuttlesschaps.com/index.html'; $infile = file( $filename ) or die( "Could not open file '$filename' for reading.\n" ); $infile = join( '', $infile ); $newfile = ereg_replace( '<(/?(b|u|i|font|ol|ul|li|img|a)( [^>]*)*)>', '[\1]', $infile ); echo $newfile; ?> ---8<---test.php---8<--- An example snippet produced by this script: . . . <table width="100%" border="0" cellpadding="3"> <tr> <td> [font face="Arial, Helvetica" size="-1"] [ul] [li] [a href="mailto:dave@thebuttlesschaps.com"]Dave Gowans[/a] - vocals, acoustic guitar, banjo, air raid siren, recorder, Casio&nbsp; [/li] . . . -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.dev (#16914) next »