Page 5 of 8

Posted: Mon Apr 16, 2007 5:27 pm
by John Cartwright
Everah suggested using preg_replace_callback instead of simply preg_replace()

Posted: Mon Apr 16, 2007 5:29 pm
by ziggy3000
oh... :roll:

how would i use that?
i dont know what preg_replace is either(all i know is that it replaces stuff). i got that part of my code in a tutorial. i read some examples in the manual, but it's pretty confusing to me (for now...)

Posted: Mon Apr 16, 2007 5:35 pm
by John Cartwright
I personally can't walk you through every step of the way. There is a ton of documentation on the function that I'm sure if you dug around you'd figure out how to work it. Have you read the user comments?

Posted: Mon Apr 16, 2007 5:40 pm
by ziggy3000
i was just reading that, but this seems intermediate->advanced range to me but this is what i get so far

preg_replace =
Php Manual wrote:mixed preg_replace ( mixed $pattern, mixed $replacement, mixed $subject [, int $limit [, int &$count]] )
Searches subject for matches to pattern and replaces them with replacement.
preg_replace_callback =
Php Manual wrote:The behavior of this function is almost identical to preg_replace(), except for the fact that instead of replacement parameter, one should specify a callback.
what does preg_replace_callback do that preg_replace doesn't?

Posted: Mon Apr 16, 2007 5:41 pm
by RobertGonzalez
preg_replace() replaces one item with another according to regular expression matching. preg_replace_callback() replaces one item with another according to regular expression matching while running it through another function. The code I offered uses that as well.

I understand and appreciate your zest for wanting to know how things work. However, your obsession with 'short' code will make what you want to do harder in the long run. You have code that works out of the box sitting in your lap, but for some reason you want us to teach you how the tutorial you downloaded works. I am really not sure what you are after. I apologize that I am not able to help you more, but I am sincerely at a loss as to what you need from us as it relates to your project.

Posted: Mon Apr 16, 2007 5:52 pm
by ziggy3000
so would it work like this?

Code: Select all

<?php
function bbcode($strings) {
$search2 = array(
		'#\[php\](.+?)\[\/php\]#is'
		);
	return preg_replace_callback($search2,hc($1,1,1),$strings);
}
?>

Posted: Mon Apr 16, 2007 6:00 pm
by RobertGonzalez
look at the examples in the manual for preg_replace_callback().

Posted: Mon Apr 16, 2007 6:01 pm
by ziggy3000
i tried it, but i just came across something really stupid. i used $1 in the function, and i know that a variable cant start with a number, so how would i use it with this tutorial
http://www.tutorialninja.com/viewtopic.php?f=7&t=27

Posted: Mon Apr 16, 2007 6:03 pm
by ziggy3000
so is this correct?

Code: Select all

<?php
function bbcode($strings) {
	echo preg_replace_callback('#\[php\](.+?)\[\/php\]#is', hc($1,1,1), $strings);
}
?>

Posted: Mon Apr 16, 2007 6:30 pm
by RobertGonzalez
Read the examples. They give you everything you need to use a callback function in the preg_replace context.

Posted: Mon Apr 16, 2007 6:37 pm
by ziggy3000
i am reading the manual, but all i want to know is that the code i typed is in the correct format, and will work when parsed(this is my first time using this function after all...)

Posted: Mon Apr 16, 2007 6:46 pm
by RobertGonzalez
If you read the example in the manual you will see that your code is not right. Look at how the examples show the use of the call back function, then make your code mimic the example.

Posted: Mon Apr 16, 2007 9:20 pm
by ziggy3000

Code: Select all

<?php
$string = '[php]<?php //code ?>[/php]';

function hc()
{
	//code...
}

echo preg_replace_callback('#\[php\](.+?)\[\/php\]#is', "hc", $string);

?>
this??

Posted: Mon Apr 16, 2007 9:46 pm
by Kieran Huggins
Thread summary (save yourselves 4 pages!) :
how do I do x?
use x()?
how does the x() function work?
read the manual.
what's a manual?
I tried typing in "http://php.net/manual" but it doesn't work. PHP tells me your dumb code doesn't work :roll:
it's a web address, use a browser.
I don't have your browser, can you come type it in for me?
use this code:

Code: Select all

<?php 
x();
?>
I prefer my own code :roll: .
Then use your own code
My code doesn't work :roll: :roll: :roll: !!!11! lolroflwwjdbbq
:banghead:

Posted: Mon Apr 16, 2007 9:51 pm
by ziggy3000
ok... about 2/3 of that is wrong. i never asked what is a manual, or how to create a function. i did ask about the callback function, but that was only 3 or 4 posts... :roll: and the last third well.... i am combining his code and my code, so in the end, it works better, and shorter code!