RE: [PHP] preg_replace works on normal string but not with a string build from a file

From: Date: Tue, 03 Dec 2002 18:05:53 +0000
Subject: RE: [PHP] preg_replace works on normal string but not with a string build from a file
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-126890@lists.php.net to get a copy of this message
> take a look at the following code > basic problem is, it works with the custom build string in the script but > not if get the code from a file. > I allways used ereg_replace but after installing a newer version of php > (4.2.2) it became very slow, so now I'm trying preg_replace > > please help !!! > kind regards Jeroen olthof, > > <?php > $opentag = '<!-- #LOOP-START:aaa -->'; > $closetag = '<!-- #LOOP-END:aaa -->'; > > // building the string in this script will give the wanted result > $string = '<?xml version="1.0" encoding="iso-8859-1"?>'; [snip] > $string .= '</html>'; > > // building the string from a text file won't give the wanted result; > // uncomma the next file for testing > // $string = implode(false,@file('./loadlooptest2.html')); > > echo "<p>--result------------------</p>\n\n\n"; > echo $result = preg_replace("/.*$opentag(.*)$closetag.*/", '\\1',$string); > echo "\n\n\n<p>--modified----------------</p>\n\n\n"; > echo $modified_template = ereg_replace("$opentag.*$closetag", > '{replaced}', > $string); Well, that string and the file aren't exactly the same. The file has newlines in it, the string you're creating does not. So in the string, it's all on the same line, while it's on separate lines in the file. Preg_replace may be looking for that pattern on a single line. The /m modifier may help in this circumstance, I'm not sure though. ---John Holmes...

« previous php.general (#126890) next »