PDA

View Full Version : Preg_replace..


TouchingVirus
04-17-2004, 10:00 PM
I cant understand the thing at all...

ereg_replace, preg_replace..i just dont get it..

I read the format on php.net and everything, its just all that ^# and ^|#@ things that i dont get..

Anyone care to share the light with a little tutorial?? :D

assassingod
04-17-2004, 10:08 PM
Take alook at the second example at http://uk.php.net/manual/en/function.preg-replace.php

The 1st parameter is what the function will look for in the defined string, the 2nd parameter is what will be replaced if there are matches in the string, and the 3rd parameter is the string you define.

I,e

<?php

// my string that i am making
$string = "assassingod is the best at everything";

// what its looking for and if theres a match itll be replaced
$patterns[0] = "/best/";
$patterns[1] = "/at everything/";

// what is going to be inplace of what has been removed
$replacements[0] = "worst";
$replacements[1] = ".";

echo preg_replace($patterns, $replacements, $string);

?>


The output of that will be

assassingod is the worst .


(Of course that's a false statement;))

TouchingVirus
04-17-2004, 10:41 PM
Ok, thanks for that assassingod..

What about 3 and 4 and 5..


"/(<\/?)(\w+)([^>]*>)/e",
"'\\1'.strtoupper('\\2').'\\3'"

Like what is all that...that is what confuses me :D

Dean C
04-18-2004, 11:04 AM
Those are regular expression pattern matchers. They were taken from perl. As much as I I'd like to sit down an explain to you about them I don't know much about them myself yet ;) In fact it would take anyone some time to explain it. I suggest looking for some tutorials on 'PHP regular expressions' and reading the section in the PHP manual about them. They're quite hard to get your head around :)

TouchingVirus
04-18-2004, 03:48 PM
thanks for the advice dean :)