Page 1 of 1

String replacement question.

Posted: Fri Jan 09, 2009 3:51 pm
by kaisellgren
Hello,

I have an irritating problem. Let's say we have this string:

"Blah X blah X blah X"

I want to replace all X's with custom text from the user. User submits 3 custom params, $x1,$x2,$x3 through GET. So

Code: Select all

str_replace('X',$x1);
Will obviously replace all X's. I already made a function that replaces the first X found. But now I have a new problem, if the user data lets say $x1 contains letter X, then that will get replaced by the $x2 :/

Any ideas to avoid this?

Re: String replacement question.

Posted: Fri Jan 09, 2009 4:02 pm
by Mark Baker
str_replace(' X ',' new text ',$string);

Re: String replacement question.

Posted: Fri Jan 09, 2009 4:20 pm
by kaisellgren
Mark Baker wrote:str_replace(' X ',' new text ',$string);
That won't work. It replaces all X's with new text. Not the first occurence.

EDIT:

Here's the deal.

Blah X Blah X Blah X

Code: Select all

$x1 = $_GET...
$x2 = ...
$x3 = ...
Now replace first X with $x1, then second X with $x2, ... but basic str_replace replaces ALL x's, not first /second. So I made this function:

Code: Select all

public static function str_replace_count($search,$replace,$subject,$times) {
    $subject_original=$subject;
    $len=strlen($search);
    $pos=0;
    for ($i=1;$i<=$times;$i++) {
        $pos=strpos($subject,$search,$pos);
        if($pos!==false) {
            $subject=substr($subject_original,0,$pos);
            $subject.=$replace;
            $subject.=substr($subject_original,$pos+$len);
            $subject_original=$subject;
        } else {
            break;
        }
    }
    return($subject);
}
But, if $x1 equals to lets say "hey mrx!" then it becomes

Blah hey mrx X blah X

And now the second X replacement will replace the mrx not the individual X.

Does anyone understand?

Re: String replacement question.

Posted: Sat Jan 10, 2009 9:53 am
by kaisellgren
Hmm I am thinking about escaping all X's. Then only nonescaped will be replaced. The only bad thing in this is the decreased speed. I would need to loop through the text :/

Re: String replacement question.

Posted: Sat Jan 10, 2009 10:16 am
by Chalks
I would probably use regex to do this, and I would make the Xs look like something that doesn't normally show up in every day text. Like... bbcode [X]. The regex to find the [X] would be /\[x\]/i or if you want it case sensitive /\[X\]/. Then use preg_replace() which allows you to limit the number of times it replaces things.

Re: String replacement question.

Posted: Sat Jan 10, 2009 10:17 am
by kaisellgren
Chalks wrote:I would probably use regex to do this, and I would make the Xs look like something that doesn't normally show up in every day text. Like... bbcode [X]. The regex to find the [X] would be /\[x\]/i or if you want it case sensitive /\[X\]/. Then use preg_replace() which allows you to limit the number of times it replaces things.
But it still won't prevent the problem if the keyword is found at the text...

Re: String replacement question.

Posted: Sat Jan 10, 2009 10:22 am
by Chalks
it won't find a match in your replacement string, if that's what you mean. If you mean it won't find a match in your initial string then... what's the point?

Re: String replacement question.

Posted: Sat Jan 10, 2009 10:45 am
by kaisellgren
Chalks wrote:it won't find a match in your replacement string,
It will, since the replacement will happen repetitious. So for the first replacement, it will not match it, but the second, third, etc may match.

Anyway, I solved this problem with RegEx lookahead.