String replacement question.

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

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

String replacement question.

Post 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?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: String replacement question.

Post by Mark Baker »

str_replace(' X ',' new text ',$string);
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: String replacement question.

Post 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?
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: String replacement question.

Post 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 :/
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: String replacement question.

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: String replacement question.

Post 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...
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: String replacement question.

Post 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?
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: String replacement question.

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