我有这样的事情:
$text = 'Getting the right kitchen appliances and home appliances for your situation is important to us.We want to make sure you are served to the best of our ability. Please feel free to contact us at any time with any inquiries you may have: <h3 style="text-align: left;"><span style="color: #0000ff;"><span style="color: #3366ff;"><span style="color: #0000ff;"><span style="color: #000000;"><strong> </strong>General inquiries (**please include your location**): </span></span></span></span>';
和preg_replace
:
preg_replace(
'/<span style="color(.+?)">(.+?)<\/span>/s',
"<span>$2</span>",
$text
);
仅替换第一次出现。如何更改它以替换此模式的每次出现?
答案 0 :(得分:1)
$text = 'Getting the right kitchen appliances and home appliances
for your situation is important to us.We want to make sure you are
served to the best of our ability. Please feel free to contact us
at any time with any inquiries you may have: <h3 style="text-align: left;">
<span style="color: #0000ff;"><span style="color: #3366ff;">
<span style="color: #0000ff;"><span style="color: #000000;"><strong>
</strong>General inquiries (**please include your location**):
</span></span></span></span>';
$text = preg_replace("/<span[^>]+\>/i", '<span style="\$2">\$3</span>', $text);
输出:
Getting the right kitchen appliances and home appliances for your situation is important to us.We want to make sure you are served to the best of our ability. Please feel free to contact us at any time with any inquiries you may have: <h3 style="text-align: left;"><span style="$2">$3</span><span style="$2">$3</span><span style="$2">$3</span><span style="$2">$3</span><strong> </strong>General inquiries (**please include your location**): </span></span></span></span>
答案 1 :(得分:0)
我认为你只想从文本中跳过颜色属性,那么为什么不在preg_replace中使用颜色而不是完整的span。 试试这个:
preg_replace(
'/color(.+?);/s',
"",
$text);