Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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 带有样式并替换为随机字符串的精确html元素的正则表达式模式_Php_Regex_Recursion - Fatal编程技术网

Php 带有样式并替换为随机字符串的精确html元素的正则表达式模式

Php 带有样式并替换为随机字符串的精确html元素的正则表达式模式,php,regex,recursion,Php,Regex,Recursion,我是stackoverflow的新手 所以我有一个html文件,它有很多一些文本 它们的数量超过100或200左右。我想用随机字符串替换span的内部文本 但是我遇到了两个问题,第一个问题是我找不到合适的正则表达式模式来查找带有内联style=“display:none” 但是我可以使用/(.*)/获取所有跨度文本。第二个问题来了。我正在使用php,下面是我的代码 preg_match_all('/<span (.*?)>(.*?)<\/span>/', $fil

我是stackoverflow的新手

所以我有一个html文件,它有很多
一些文本

它们的数量超过100或200左右。我想用随机字符串替换span的内部文本

但是我遇到了两个问题,第一个问题是我找不到合适的正则表达式模式来查找带有内联
style=“display:none”

但是我可以使用
/(.*)/
获取所有跨度文本。第二个问题来了。我正在使用php,下面是我的代码

    preg_match_all('/<span (.*?)>(.*?)<\/span>/', $file, $matches);
    foreach ($matches as $value) {
        $string = str_replace($value, RandomString(), $file);
    } 
但它返回500个错误

那么我如何一步一步地替换找到的匹配项呢

示例

不更改文本
更改文本
更改文本
我希望它是这样的

<span>not to change text</span> 
<span style="display:none">some random string</span>
<span style="display:none">other ddifferent random string</span>

不更改文本
一些随机字符串
其他不同的随机字符串

正则表达式是强制性的

最好使用

但您编写的正则表达式是强制性的,所以这里有一个工作示例:

preg_match_all('/<span .*?display:none.*?>(.*?)<\/span>/', $string, $matches);
foreach($matches[1] as $match) {
    $string = preg_replace('/>' . $match . '</', '>' . rand() . '<', $string, 1);
}
preg_match_all(“/(.*?/”,$string,$matches);
foreach($matches[1]作为$match){
$string=preg_replace(“/>”。$match.'.rand()。'
<span>not to change text</span> 
<span style="display:none">some random string</span>
<span style="display:none">other ddifferent random string</span>

preg_match_all('/<span .*?display:none.*?>(.*?)<\/span>/', $string, $matches);
foreach($matches[1] as $match) {
    $string = preg_replace('/>' . $match . '</', '>' . rand() . '<', $string, 1);
}