Php 替换html标记dosn';t工作正常(预更换)

Php 替换html标记dosn';t工作正常(预更换),php,Php,嗨,我用这个preg_替换: preg_replace("/<font(.*?)>$key2<\/font>/","<b>$key2</b>",$out,1) preg_replace(“/$key2/”、“$key2”、$out,1) 但它取代的不仅仅是这种模式!有时,使用此函数可以删除多行输入!我只需要更换第一个箱子 我需要完全替换这个: <font style='color:red;text-decoration:line-thro

嗨,我用这个preg_替换:

preg_replace("/<font(.*?)>$key2<\/font>/","<b>$key2</b>",$out,1)
preg_replace(“/$key2/”、“$key2”、$out,1)
但它取代的不仅仅是这种模式!有时,使用此函数可以删除多行输入!我只需要更换第一个箱子 我需要完全替换这个:

<font style='color:red;text-decoration:line-through' >$key2</font>
$key2
惠特:

<b>$key2</b>
$key2

如果您有固定的
样式,请按照以下步骤操作。使用
str\u replace

$in_val = "font style='color:red;text-decoration:line-through'";
echo str_replace("font style='color:red;text-decoration:line-through'","bold",$in_val); //instead of echoing, you can store it in a variable as well to use it further.
现在,对于结束标记,使用下面的代码

$in_val_2 = '/font';
echo str_replace("/font","/bold",$in_val_2);

这将为您替换代码。

如果需要替换静态字符串,为什么要使用regex?只需使用简单的字符串搜索。如果您需要为HTML标记使用正则表达式,这是一个非常复杂的主题,请参阅您是否为所有标记都使用了固定的
样式?当我使用上述字符串dostn无效或显示错误时,您是否考虑过使用DOM͜pa̡r͏ser̵?@BM。@MohammedAkhtarZuberi是的。