如何使用PHP的preg_replace替换文本中所有出现的字符串,除了那些在html的A标签内找到的字符串,即:rest
例如,我想将单词 color 替换为 blue 。
**Original text**: Tim's favorite color is color. Color is also Bill's favorite.
**Converted text**: Tim's favorite color is blue. Blue is also Bill's favorite.
答案 0 :(得分:0)
我想这里最好的方法是用指定的单词替换该单词的所有实例。然后你应该用原始单词
替换A标签内的文本所以使用你的例子:
**Original text**:
Tim's favorite [color] is color.
Color is also Bill's favorite.
**Interim step text**:
Tim's favorite [blue] is blue.
Blue is also Bill's favorite.
**Converted text**:
Tim's favorite [color] is blue.
Blue is also Bill's favorite.
使用此方法会遇到的问题是:
因此我没有包含任何代码,但我希望这能让你走上正轨。