[solved] php highlight function help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Everah suggested using preg_replace_callback instead of simply preg_replace()
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post 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...)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post 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);
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

look at the examples in the manual for preg_replace_callback().
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post 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
Last edited by ziggy3000 on Mon Apr 16, 2007 6:09 pm, edited 1 time in total.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post 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);
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Read the examples. They give you everything you need to use a callback function in the preg_replace context.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post 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...)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post by ziggy3000 »

Code: Select all

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

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

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

?>
this??
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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:
ziggy3000
Forum Contributor
Posts: 205
Joined: Fri Mar 23, 2007 3:04 pm

Post 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!
Post Reply