我想替换svg-code-string中的特定标记。标签看起来像这样:
<rect x="0" fill="#C1984F" width="120" height="80"/>
但颜色应该用不同的颜色替换......
我认为它应该是这样的:
$thumbContents = preg_replace('<rect x="0" fill="*" width="120" height="80"/>', '<rect x="0" fill="'.$selectedColor.'" width="120" height="80"/>', $thumbContents);
但我不知道如何编写正则表达式模式。
答案 0 :(得分:1)
如果你坚持使用正则表达式......
$tag = '<rect x="0" fill="#C1984F" width="120" height="80"/>';
$newc = 'red';
echo preg_replace('~(<rect[^<>]+?fill=")([^"]+)~', "$1$newc", $tag);
模式意味着
(<rect[^<>]+?fill=") - group 1: "<rect" then anything (but not <>) until fill="
([^"]+) - group 2: anything but not a quote (i.e. the color value)
的更换:
$1 - whatever has been captured by the group 1
$newc - new color value
答案 1 :(得分:1)
你可以试试这个:
<script>
$(document).ready(function(){
var list = document.getElementsByTagName("rect")[0];
alert(list.getAttribute('fill'));// change the value here
});
</script>
<rect x="0" fill="#C1984F" width="120" height="80"/>
抱歉,但它是在javascript中。