Php 正在替换尚未替换的字符串

Php 正在替换尚未替换的字符串,php,Php,所以我有这样一个文本: "word1 word2 word3 etc" [ "word1 word2" => "<a href="someurl">word1 word2</a>", "word2" => "<a href="someurl">word2</a>", "word3" => "<a href="someurl">word3</a>

所以我有这样一个文本:

"word1 word2 word3 etc"
[    
     "word1 word2" => "<a href="someurl">word1 word2</a>",
     "word2"       => "<a href="someurl">word2</a>",
     "word3"       => "<a href="someurl">word3</a>" 
]
<a href="someurl">word1 word2</a> word3 etc
"<a href="someurl">word1 <a href="someurl">word2</a></a> word3 etc"
                            ^^^ another replacement inside "word1 word2"
我有一个带有一组重放的阵列,我必须像这样携带:

"word1 word2 word3 etc"
[    
     "word1 word2" => "<a href="someurl">word1 word2</a>",
     "word2"       => "<a href="someurl">word2</a>",
     "word3"       => "<a href="someurl">word3</a>" 
]
<a href="someurl">word1 word2</a> word3 etc
"<a href="someurl">word1 <a href="someurl">word2</a></a> word3 etc"
                            ^^^ another replacement inside "word1 word2"
[
“word1 word2”=>“”,
“word2=>”,
“word3=>”“
]
基本上,对于一些单词(或它们的组合),我必须添加一些标签

我需要避免这种情况,因为“word1 word2”已经被这样替换:

"word1 word2 word3 etc"
[    
     "word1 word2" => "<a href="someurl">word1 word2</a>",
     "word2"       => "<a href="someurl">word2</a>",
     "word3"       => "<a href="someurl">word3</a>" 
]
<a href="someurl">word1 word2</a> word3 etc
"<a href="someurl">word1 <a href="someurl">word2</a></a> word3 etc"
                            ^^^ another replacement inside "word1 word2"
word3等
我需要避免它变成这样:

"word1 word2 word3 etc"
[    
     "word1 word2" => "<a href="someurl">word1 word2</a>",
     "word2"       => "<a href="someurl">word2</a>",
     "word3"       => "<a href="someurl">word3</a>" 
]
<a href="someurl">word1 word2</a> word3 etc
"<a href="someurl">word1 <a href="someurl">word2</a></a> word3 etc"
                            ^^^ another replacement inside "word1 word2"
“word3等”
^^^“word1 word2”中的另一个替换项
如何避免替换在其他替换中已经找到的较小字符串的替换

使用str_替换的实时代码不工作:
$array=[
“word1 word2”=>“”,
“word2=>”,
“word3=>”“
];
$txt=“word1 word2 word3等”;
echo str_replace(数组_键($array)、数组_值($array)、$txt);

正确输出:

不确定这是否是最好的解决方案,但您可以将单词组合替换为完全不同的单词,然后在完成后将其替换回原始形式

范例

$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;
$array=[
“word1 word2”=>“”,
“word2=>”,
“word3=>”,
];
$array2=[
“***某物***其他***”=>“单词1单词2”,
];
$txt=“word1 word2 word3等”;
$txt=str_替换(数组_键($array)、数组_值($array)、$txt);
$txt=str_替换(数组_键($array2)、数组_值($array2)、$txt);
echo$txt;
试试这个:

$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
$array=[
“word1 word2”=>“”,
“word2=>”,
“word3=>”“
];
$txt=“word1 word2 word3等”;
foreach($word=>$replacement形式的数组){
如果(!stripos($txt,“>$word$word”)&&!stripos($txt,$word可能会在替换数组集的键处添加“空格”,并执行
stru replace()
。类似的情况可能是:

<?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." ");

我认为这可以通过使用正则表达式来解决,你试过了吗?不,我不能这样做,因为我可以使用一些标点符号:word1 word2。Word3是的,这是一种方法,但感觉不是这样elegant@giò我同意你的看法,这可能不是最优雅的解决方案,但它很有效,而且相对容易理解和。