preg_replace works on normal string but not with a string build from a file
| From: | Jeroen Olthof | Date: | Tue, 03 Dec 2002 15:28:34 +0000 |
| Subject: | preg_replace works on normal string but not with a string build from a file | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-126871@lists.php.net to get a copy of this message | ||
hi,
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"?>';
$string .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
$string .= '<html xmlns="http://www.w3.org/1999/xhtml">';
$string .= '<head>';
$string .= '<title>Untitled Document</title>';
$string .= '<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />';
$string .= '</head>';
$string .= '';
$string .= '<body>';
$string .= '<!-- #LOOP-START:aaa -->';
$string .= '<table width="100%" border="0">';
$string .= ' <tr> ';
$string .= ' <td>aaa</td>';
$string .= ' <td>aaa</td>';
$string .= ' <td>aaa</td>';
$string .= ' <td>aaa</td>';
$string .= ' </tr>';
$string .= '</table>';
$string .= '<!-- #LOOP-END:aaa -->';
$string .= '</body>';
$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);
?>
here is the xhtml file (copy past it in a file called loadlooptest2.html)
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<table width="100%" border="1">
<!-- #LOOP-START:aaa -->
<tr>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
</tr>
<!-- #LOOP-END:aaa -->
<tr>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
<td>aaa</td>
</tr>
</table>
<!-- #LOOP-END:aaa -->
</body>
</html>