PHP 4.0 Bug #3850 Updated: ereg_replace broken when pattern string is too long
| From: | Bug Database | Date: | Thu, 16 Mar 2000 22:30:20 +0000 |
| Subject: | PHP 4.0 Bug #3850 Updated: ereg_replace broken when pattern string is too long | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-16915@lists.php.net to get a copy of this message | ||
ID: 3850
Updated by: torben
Reported By: brian@phorum.org
Status: Closed
Bug Type: Misbehaving function
Assigned To:
Comments:
(note: sorry about doubling this up on php-dev: brainfart on my part)
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
[/li]
. . .
Full Bug description available at: http://bugs.php.net/version4/?id=3850