我有一个带有inlineXML标记的字符串,我想用span替换它,并转储其余未被标记包围的其余字符串。这是我的代码。
$string = "My name is <Name>John Farro</Name>. I live in <Location>New York City<Location>."
$new = str_replace(array("<Name>", "</Name>", "<Location>", "</Location>"), array("<span style="color:blue">", "</span>", "<span style="color:red">", "</span>"), "$string");
echo $new;
Result: "My name is <span style="color:blue">John Farro</span>. I live in <span style="color:red">New York City</span>."
我想要的只是这样:
<span style="color:blue">John Farro</span>
<span style="color:red">New York City</span>
答案 0 :(得分:0)
您需要使用正则表达式来删除HTML标记
/<\s*span[^>]*>(.*?)<\s*\/\s*span>/
你需要的PHP代码:
<?php
$str = 'My name is <span style="color:blue">John Farro</span>. I live in <span style="color:red">New York City</span>.';
preg_match_all("/<\s*span[^>]*>(.*?)<\s*\/\s*span>/", $str, $matches);
print_r($matches[0]);
?>
结果:
的引用: