Forum Moderators: coopster
function BBCode ($str) {
$str = htmlentities($str);$search = array(
'/\[quote\=(.*?)\]/is'
);
$replace = array(
quoteNum('$1')
);
$str = preg_replace ($search, $replace, $str);
return str_replace("\n", "<br>\n", $str);
}
Unfortunately, the value it's sending to the function will always be a number, but the function is reading the input as a string for some reason. I've tried typecasting both in the second function AND in the array with no luck, and I'm sure this is the problem, as I've simply replaced the variable in the function with the number 1 as a test with zero problems. Any help?
P.S. removing the quotes from $1 will not work because a regular variable should not begin with a number.
[edited by: TheAlbinoEthiopian at 11:39 pm (utc) on Sep. 22, 2008]
There are special variable $1...n in the preg_* functions. These variables are set before every thing else is set. So even using ' around $1 in the $replace will not stop $1 getting set to the first captured string. Is that what you want, or do you actually want to use the literal string $1?
$replace = array(
quoteNum('$1')
);
I suspect that as the $replace variable is already being used by the preg engine, that PHP is not able to call the quoteNum function and before the regex engine starts the variable $1 is useless for what you want as it is a literal string.
It would be interesting if anyone has looked at the actual workings of the source code and could confirm my hypothesis.
I would suggest that you have a look at the preg_replace_callback [uk2.php.net] function. As this would allow you access to the $1 variables and the ability to call other custom functions.