Forum Moderators: coopster

Message Too Old, No Replies

preg replace value into function

         

TheAlbinoEthiopian

11:34 pm on Sep 22, 2008 (gmt 0)

10+ Year Member



I'm running a BBCode function through my forum, and a special quoting option I'm writing requires a separate function. I'm trying to call that function through the preg_replace array like so:

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]

PHP_Chimp

9:57 pm on Sep 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the quoteNum function doing? As you are passing the result from your (.*?) in the $search into the quoteNum function.

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?

TheAlbinoEthiopian

11:53 pm on Sep 23, 2008 (gmt 0)

10+ Year Member



the quoteNum function takes the number sent to it, plugs it into some SQL queries, and returns a string. Basically, the problem is that the $1 value is not being read as a number, but as a string. Say the value is 2, if I echo it, it will print 2, if I copy the query into phpmyadmin, it runs fine and gives me the expected output, but when I run it, it doesn't find any matching rows. After some trial and error, I figured out that the problem was in the interpretation of the variable being sent to it, because it would print the right number, and simply replacing it with the number 2 worked fine.

PHP_Chimp

11:48 am on Sep 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I dont know if this is correct, but after a bit of testing I have come to the conclusion that the problem lies with
$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.

TheAlbinoEthiopian

1:47 am on Sep 26, 2008 (gmt 0)

10+ Year Member



Thanks PHP_Chimp, that worked perfectly!