giò
giò

Reputation: 3600

Replacing string that have not been already replaced

So I have a text like this:

"word1 word2 word3 etc"

And I have an array with a set of replacment that I have to carry like this:

[    
     "word1 word2" => "<a href="someurl">word1 word2</a>",
     "word2"       => "<a href="someurl">word2</a>",
     "word3"       => "<a href="someurl">word3</a>" 
]

Basically for some of the words (or a combination of them) I have to add some tags.

I need to avoid that since "word1 word2" is already replaced like this:

<a href="someurl">word1 word2</a> word3 etc

I need to avoid that it becomes like this:

"<a href="someurl">word1 <a href="someurl">word2</a></a> word3 etc"
                            ^^^ another replacement inside "word1 word2"

How can I avoid replacemnet of smaller strings already found inside other replacement ?

Live code using str_replace not working:

$array = [    
     "word1 word2" => "<a href='someurl'>word1 word2</a>",
     "word2"       => "<a href='someurl'>word2</a>",
     "word3"       => "<a href='someurl'>word3</a>" 
];

$txt = "word1 word2 word3 etc";

echo str_replace(array_keys($array),array_values($array),$txt);

http://sandbox.onlinephpfunctions.com/code/85fd62e88cd0131125ca7809976694ee4c975b6b

Correct output:

<a href="someurl">word1 word2</a> <a href="someurl">word3</a> etc

Upvotes: 3

Views: 67

Answers (3)

anon
anon

Reputation: 361

maybe adding "space" at key of set of replacement array and do str_replace(). something like this maybe:

<?php
    //Enter your code here, enjoy!

    $array = [    
         "word1 word2 " => "<a href='someurl'>word1 word2</a>",
         "word2 "       => "<a href='someurl'>word2</a>",
         "word3 "       => "<a href='someurl'>word3</a>" 
    ];

    $txt = "word1 word2 word3 etc";

    echo str_replace(array_keys($array),array_values($array),$txt." ");

Upvotes: 0

failedCoder
failedCoder

Reputation: 1424

Try this:

$array = [    
 "word1 word2" => "<a href='someurl'>word1 word2</a>",
 "word2"       => "<a href='someurl'>word2</a>",
 "word3"       => "<a href='someurl'>word3</a>" 
];

 $txt = "word1 word2 word3 etc";

foreach ($array as $word => $replacement) {
   if (!stripos($txt, ">$word<") && !stripos($txt, ">$word") && !stripos($txt, "$word<") ){
    $txt = str_replace($word, $replacement, $txt);
   }
}
echo $txt;

// output: <a href='someurl'>word1 word2</a> <a href='someurl'>word3</a> etc

Basically, before you replace the word, check if it's wrapped in tags already

Upvotes: 1

Peter M
Peter M

Reputation: 1059

Not sure whether it's the best solution but you could replace the combination of words for something entirely different, then when you're done replace that back to it's original form.

Example

$array = [    
     "word1 word2" => "<a href='someurl'>***something***else***</a>",
     "word2"       => "<a href='someurl'>word2</a>",
     "word3"       => "<a href='someurl'>word3</a>",
];

$array2 = [    
     "***something***else***" => "word1 word2",
];

$txt = "word1 word2 word3 etc";
$txt = str_replace(array_keys($array),array_values($array),$txt);
$txt = str_replace(array_keys($array2),array_values($array2),$txt);

echo $txt;

Upvotes: 0

Related Questions