...

View Full Version : ereg_replace question?



icydash
10-22-2009, 07:08 AM
Hi! So I'm trying to use ereg_replace to replace every instance of a string within something else within a larger string... like ya do. Here's where the issue comes in, the string i'm trying to replace with another string has stars (*) in it, and i'm not sure how to do that. For instance:

$str1 = "test****";
$str2 = "test1234";
$conversation = "the man said test**** down by the bay.";

$output = ereg_replace($str1, $str2, $conversation);

Apparently, ereg_replace doesn't like finding strings with * in it, because * means something else. How do i circumvent this so i can find all the occurrences of my $str1, above even though it has a * in it?

Phil Jackson
10-22-2009, 07:39 AM
use preg_replace.


$str1 = "test****";
$str1 = preg_quote($str1, "#");
$str2 = "test1234";
$conversation = "the man said test**** down by the bay.";

$output = preg_replace("#".$str1."#is", $str2, $conversation);

icydash
10-22-2009, 02:48 PM
Thanks!

What does the preg_quote, and the the # do? and #is? can you explain it a little big please?

ninnypants
10-22-2009, 03:55 PM
preg_quote() escapes all regular expression characters in the provided string, and "#".$string1." #is" is just a regular expression you can use other characters like # or @ in place of /
'i' means that the match is case insensitive and 's' means that the regular expression should be treated as a single line.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum