#25512 [Bgs]: preg_replace Offset error

From: Date: Fri, 12 Sep 2003 13:00:58 +0000
Subject: #25512 [Bgs]: preg_replace Offset error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-47513@lists.php.net to get a copy of this message
ID: 25512 User updated by: koni at 2complex dot net Reported By: koni at 2complex dot net Status: Bogus Bug Type: Reproducible crash Operating System: Windows XP PHP Version: 4.3.3 New Comment: hey, this is a SERIOUS BUG REPORT, PHP can NOT preg_replace this code. I am sorry if this was the text that the user submitted in my forum and I dont know how to reproduce the problem with another code so I had to submit the original Code. I am sorry if you feel offended but I think you should be a lot more objective about this problem. Forget about the content and look at the bug. Previous Comments: ------------------------------------------------------------------------ [2003-09-12 08:19:34] sas@php.net Nice signature. Go away ------------------------------------------------------------------------ [2003-09-12 07:15:15] koni at 2complex dot net Description: ------------ I wanted to write a vbb code style for a new forum and I used nl2br to format the mysql data into html. As there was a special [code] ... [/code] area in my vbb code which used the html <pre> ... </pre> command the format the spaces correctly, I saw that nl2br changes \n into <br />\n and therefor <pre> shows 2 lines instead of 1. As there 1. is no option for nl2br to either replace with <br /> or < br/>\n, I wrote a small Code to preg_replace the code between the [code] .. [/code] commands with a placeholder, then nl2br the whole thing and then replace the placeholder again with the code. This did work fine ... until I used the following ascii figure between the [code] commands: * g o a t s e x * g o a t s e x * g o a t s e x * g g o / \ \ / \ o a| | \ | | a t| `. | | : t s` | | \| | s e \ | / / \ --__ \ : e x \ \/ _--~~ ~--__| \ | x * \ \_-~ ~-_\ | * g \_ \ _.--------.______\| | g o \ \______// _ ___ _ (_(__> \ | o a \ . C ___) ______ (_(____> | / a t /\ | C ____)/ \ (_____> |_/ t s / /\| C_____) | (___> / \ s e | ( _C_____)\______/ // _/ / \ e x | \ |__ \_________// (__/ | x * | \ \____) `---- --' | * g | \_ ___\ /_ _/ | g o | / | | \ | o a | | / \ \ | a t | / / | | \ |t s | / / \__/\___/ | |s e | / / | | | |e x | | | | | |x * g o a t s e x * g o a t s e x * g o a t e x * This gave me the following: Warning: Compilation failed: missing ) at offset 1412 in c:\phpdev\www\tattered\functions.php on line 788 and line 788 is: $message = preg_replace("#".escape($match)."#si","##$counter##",$message); Every preg_replace crashes when it has to replace the above-mentioned code. Reproduce code: --------------- function escape($string) { $list = array("[" => "\[", "]" => "\]", "/" => "\/"); foreach($list as $search => $replace) { $string = str_replace($search, $replace, $string); } return $string; } preg_match_all("#\[code\]([^\[]*)\[\/code\]#si",$message,$matches); foreach($matches[0] as $counter => $match) { $message = preg_replace("#".escape($match)."#si","##$counter##",$message); } $message = nl2br($message); foreach($matches[0] as $counter => $match) { $message = preg_replace("/##$counter##/si",$match,$message); } Expected result: ---------------- g o a t s e x * g o a t s e x * g o a t s e x * g g o / \ \ / \ o a| | \ | | a t| `. | | : t s` | | \| | s e \ | / / \ --__ \ : e x \ \/ _--~~ ~--__| \ | x * \ \_-~ ~-_\ | * g \_ \ _.--------.______\| | g o \ \______// _ ___ _ (_(__> \ | o a \ . C ___) ______ (_(____> | / a t /\ | C ____)/ \ (_____> |_/ t s / /\| C_____) | (___> / \ s e | ( _C_____)\______/ // _/ / \ e x | \ |__ \_________// (__/ | x * | \ \____) `---- --' | * g | \_ ___\ /_ _/ | g o | / | | \ | o a | | / \ \ | a t | / / | | \ |t s | / / \__/\___/ | |s e | / / | | | |e x | | | | | |x * g o a t s e x * g o a t s e x * g o a t e x * Actual result: -------------- g o a t s e x * g o a t s e x * g o a t s e x * g g o / \ \ / \ o a| | \ | | a t| `. | | : t s` | | \| | s e \ | / / \ --__ \ : e x \ \/ _--~~ ~--__| \ | x * \ \_-~ ~-_\ | * g \_ \ _.--------.______\| | g o \ \______// _ ___ _ (_(__> \ | o a \ . C ___) ______ (_(____> | / a t /\ | C ____)/ \ (_____> |_/ t s / /\| C_____) | (___> / \ s e | ( _C_____)\______/ // _/ / \ e x | \ |__ \_________// (__/ | x * | \ \____) `---- --' | * g | \_ ___\ /_ _/ | g o | / | | \ | o a | | / \ \ | a t | / / | | \ |t s | / / \__/\___/ | |s e | / / | | | |e x | | | | | |x * g o a t s e x * g o a t s e x * g o a t e x * ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=25512&edit=1

« previous php.bugs (#47513) next »