Re: preg_replace works on normal string but not with a string build from a file
| From: | Jeroen Olthof | Date: | Tue, 03 Dec 2002 23:01:46 +0000 |
| Subject: | Re: preg_replace works on normal string but not with a string build from a file | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-126906@lists.php.net to get a copy of this message | ||
Thx, I had created a workarround, withs was looping through every line of
the file and doing a rtrim. But your hint pointed me to the better solution
The modifier m alone didn't do the job, because I uses a dot to match any
character. But without the s modifier it will not match a new line. So both
modifiers gets it all to work! Thx for the tip!
Kind regards
Jeroen Olthof,
"John W. Holmes" <holmes072000@charter.net> schreef in bericht
news:000801c29af6$a1a28450$7c02a8c0@coconut...
> > 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...
>
>