Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 如何替换重复符号?_Php_Regex - Fatal编程技术网

Php 如何替换重复符号?

Php 如何替换重复符号?,php,regex,Php,Regex,我有一个html内容。在我使用类似 preg_match('/adm-list-table-cell.*\"del\".*\<\/td/', $content, $zzz); $new_string = preg_replace('/(\s\/\s)/','',$zzz[0]); $content = str_replace($zzz[0], $new_string, $content); preg\u match('/adm list table cell.*\“del\”*\此pre

我有一个html内容。在我使用类似

preg_match('/adm-list-table-cell.*\"del\".*\<\/td/', $content, $zzz);
$new_string = preg_replace('/(\s\/\s)/','',$zzz[0]);
$content = str_replace($zzz[0], $new_string, $content);

preg\u match('/adm list table cell.*\“del\”*\此preg模式将删除字符串中的所有
/

$content = preg_replace('/(adm-list-table-cell[^>]*>)\\s(?:\\/\s+)+(<span class="del".*<\\/span>)\\s(?:\\/\\s+)+(<)/', '${1}${2}${3}', $content);

$content=preg\u replace('/(adm list table cell[^>]*>)\\s(?:\\/\s+)(有关StackOverflow解析html的总体建议是使用类似DomDocument的html解析器。如果您提供有关输入文本可变性的更多信息,我可能能够编写一个完善的DomDocument解决方案

在此期间,这里是一个直接的单模式
preg_replace()
调用,没有不必要的转义、最少的捕获组和贪婪的字符类,以提高效率和简洁性

代码:()()


$html=试试这个
preg_replace('/\s+/','.$zzz[0]);
你真的希望在
////ALL///////code>字符串中找到
adm列表单元格吗?为什么不
trim($string,'/')
?我有一个字符串,它在转换成html之后。在这个字符串中,我不想替换所有“/”转换为一个空符号,只有“/”,其中包含大约
span class=“del”
,因此我试图找到该span,因为我有许多
adm列表单元格
块,但只有很少的块可以使用该span
$html = <<<HTML
<td class="adm-list-table-cell align-right"><a href="iblock_element_edit.php?IBLOCK_ID=1&amp;type=news&amp;ID=2&amp;lang=ru&amp;find_section_section=-1&amp;WF=Y" title="title">2</a></td><td class="adm-list-table-cell align-left adm-list-table-cell-last"> /  /  /  / <span class="del">ALL</span> /  /  /  /  /  /  /  /  /  / </td>
HTML;

echo preg_replace('~adm-list-table-cell.*?\K[/ ]*(<span class="del".*?</span>)[/ ]*~', '$1', $html);
<td class="adm-list-table-cell align-right"><a href="iblock_element_edit.php?IBLOCK_ID=1&amp;type=news&amp;ID=2&amp;lang=ru&amp;find_section_section=-1&amp;WF=Y" title="title">2</a></td><td class="adm-list-table-cell align-left adm-list-table-cell-last"><span class="del">ALL</span></td>