Regular expressions
| From: | pete at doberad dot com | Date: | Tue, 03 Feb 2004 17:25:45 +0000 |
| Subject: | Regular expressions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-176703@lists.php.net to get a copy of this message | ||
Hi there,
I have serious probems resolving regular expressions. I wish to
replace a substring with a given pattern with results of a function
with a backreference used as argument.
SAMPLE:
function check_this_out($somevalue)
{
$url_array = '';
$i=0;
$query = "SELECT SomeRow FROM SomeTable WHERE
SomeRowID='$somevalue';";
$result = mysql_query($query) or die (mysql_error().$query);
$someow = mysql_fetch_array($result);
var_dump($somerow);
return ($query);
}
$output = 'aliquyam erat, sed diam voluptua. [link="21"]This
is a test[/link]';
echo ($output);
echo ('<br />');
$output =
ereg_replace('\[link="([[:graph:]]+)"]',check_this_out('\1'),
$output);
echo ('<br />');
var_dump ($output);
$output2 = 'aliquyam erat, sed diam voluptua.
[link="21"]This is another test[/link]';
echo ($output);
echo ('<br />');
$output2 =
ereg_replace('/\[link="([[:graph:]]+)"]/',check_this_out('\1'),
$output);
echo ('<br />');
var_dump ($output2);
The problem is, that although the $query string in this example will
echo as "SELECT SomeRow FROM SomeTable WHERE SomeRowID='21';", where
$somevalue has been replaced by '21' as a result by the regular
expressions, it will eventually render as "SELECT SomeRow FROM
SomeTable WHERE SomeRowID='\1';" when mysql is called, return the row
with SomeRowID='\1', which incidently is not the row I want... I tried
type casting to no avail.
Has anybody experienced similar problems or know how to get around
this?
My php version is somewhere between 4.3.4 and 4.3.5... a 4.3.5-dev...
Best Regards
Peter Normann
?>