Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Preg_用中间的通配符替换字符串_Php_Regex - Fatal编程技术网

Php Preg_用中间的通配符替换字符串

Php Preg_用中间的通配符替换字符串,php,regex,Php,Regex,我想替换svg代码字符串中的特定标记。标签如下所示: <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"/>', $thu

我想替换svg代码字符串中的特定标记。标签如下所示:

<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);

但是颜色应该换成不同的颜色

我认为应该是这样的:

<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);
$thumbContents=preg_replace('',$thumbContents);

但是我不知道如何编写正则表达式模式。

如果你坚持使用正则表达式来实现这个

$tag = '<rect x="0" fill="#C1984F" width="120" height="80"/>';

$newc = 'red';

echo preg_replace('~(<rect[^<>]+?fill=")([^"]+)~', "$1$newc", $tag);
您可以尝试以下方法:

<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"/>

$(文档).ready(函数(){
var list=document.getElementsByTagName(“rect”)[0];
警报(list.getAttribute('fill');//在此处更改值
});

很抱歉,它是用javascript编写的。

考虑使用DOM而不是正则表达式。只需找到
fill=“*”
的索引,并用新字符串替换(appned),而不使用正则表达式。我认为DOM在这方面非常重要。。。这是我唯一需要像这样修改标记的情况。它总是这个标签。搜索索引是不可能的,因为还有其他带有fill-attribute的标签。你应该做
~(谢谢你,伙计,但是我需要一个PHP变量,因为我用修改过的文件生成了一个下载