Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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_Xml_Regex - Fatal编程技术网

Php 正则表达式替换未包含在标记之间的单词(非html)

Php 正则表达式替换未包含在标记之间的单词(非html),php,xml,regex,Php,Xml,Regex,有限的正则表达式经验,我在PHP中使用preg_replace 我想替换不在[no glossary]之间的指定“单词”。。。[/no glossary]标签。如果它们不是“单词”和标记之间的空格,或者它们是“单词”后面的空格,但是如果我在单词前面加了空格(例外),那么我的表达式就有效了 这些工作: $html = '<p>Do not replace [no-glossary]this[/no-glossary] replace this.</p>'; $html =

有限的正则表达式经验,我在PHP中使用preg_replace

我想替换不在[no glossary]之间的指定“单词”。。。[/no glossary]标签。如果它们不是“单词”和标记之间的空格,或者它们是“单词”后面的空格,但是如果我在单词前面加了空格(例外),那么我的表达式就有效了

这些工作:

$html = '<p>Do not replace [no-glossary]this[/no-glossary] replace this.</p>';
$html = '<p>Do not replace [no-glossary]this [/no-glossary] replace this.</p>';
$html='不要替换[no glossary]这个[/no glossary]替换这个。

'; $html='不要替换[no glossary]这个[/no glossary]替换这个。

这并不是:

$html = '<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>';
$html='不要替换[no glossary]这个[/no glossary]替换这个。

';
使用的模式逐部分解释

/                      - find
(?<!\[no-glossary\])   - Not after the [no-glossary] tag
[ ]*                   - Followed by 0 or more spaces (I think this is the problem)
\b(this)\b             - The word "this" between word boundaries
[ ]*                   - Followed by 0 or more spaces
(?!\[\/no-glossary\])  - Not before the [/no-glossary] tag
/
/-查找
(?
代码如下:

$pattern = "/(?<!\[no-glossary\])[ ]*\b(this)\b[ ]*(?!\[\/no-glossary\])/"; 
$html = '<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>';
$html = preg_replace($pattern, "that", $html);

print $html;
$pattern=“/(?试试这个:
([^\[\]])这个([^\[\]])

当然,你需要在“this”这个词上应用你真正需要的东西。

试试这个:

\b(this)\b(?!(?:(?!\[no-glossary\]).)*?\[/no-glossary\])

如果后面紧跟着
[/no glossary]
,这将排除替换
,除非它首先遇到
[no glossary]

尝试此操作,它只替换
单词

$pattern = '%([^\da-zA-Z]+)this([^\da-zA-Z]+)%si';
$html = '<p>Do not replace [no-glossary]this sdf[/no-glossary] thisreplace<p> replacethis.</p>replace this.</p>';
function Replace1($M){
//print_r($M);
    return $M[1]."that".$M[2];
}
$html = preg_replace_callback($pattern,"Replace1",$html);
print $html;
$pattern='%([^\da-zA-Z]+)这个([^\da-zA-Z]+)%si';
$html='不要替换[no glossary]这个sdf[/no glossary]这个替换替换这个。

替换这个。

'; 函数替换1($M){ //印刷费(百万美元); 返回$M[1]。“$M[2]; } $html=preg_replace_回调($pattern,“Replace1”,$html); 打印$html;
输出:

<p>Do not replace [no-glossary]that sdf[/no-glossary] thisreplace<p> replacethis.</p>replace that.</p>
不要替换sdf[/no glossary]this替换的[no glossary]this替换这个。

替换那个


只需捕获空白:

$subject = <<<LOD
<p>Do not replace [no-glossary]this[/no-glossary] replace this.</p>
<p>Do not replace [no-glossary]this [/no-glossary] replace this.</p>
<p>Do not replace [no-glossary] this[/no-glossary] replace this.</p>
<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>
LOD;
$pattern = '`(?<!\[no-glossary])( *+)\bthis\b( *+)(?!\[/no-glossary])`';
echo $subject.'<br/>';
echo preg_replace($pattern,"$1rabbit$2",$subject); ?>

$subject=在玩了一点RegEx模式之后,我发现RegEx PCRE引擎有一些限制,所以我从另一个角度来解决这个问题:

  • 匹配所有
    [no glossary]此[/no glossary]
  • 过滤结果
  • 这可以通过
    preg\u replace\u callback()完成。

    PHP 5.3+必填项

    $pattern = "/\[no-glossary\][ ]*\bthis\b[ ]*\[\/no-glossary\]|this/"; 
    $html = '<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>';
    
    $html = preg_replace_callback($pattern, function($match){
        if($match[0] == 'this'){
            return('that');
        }else{
            return($match[0]);
        }
    }, $html);
    
    print $html;
    
    $pattern=“/\[no glossary\][]*\b此\b[]*\[\/no glossary\]]124;此/”;
    $html='不要替换[no glossary]这个[/no glossary]替换这个。

    ; $html=preg\u replace\u回调($pattern,function($match){ 如果($match[0]==“this”){ 返回(“该”); }否则{ 返回($match[0]); } },$html); 打印$html;
    如果您没有运行PHP5.3+:

    $pattern = "/\[no-glossary\][ ]*\bthis\b[ ]*\[\/no-glossary\]|this/"; 
    $html = '<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>';
    
    $html = preg_replace_callback($pattern, 'replace_function', $html);
    
    function replace_function($match){
        if($match[0] == 'this'){
            return('that');
        }else{
            return($match[0]);
        }
    }
    print $html;
    
    $tag = 'no-glossary';
    $find = 'this';
    $replace = 'that';
    
    $pattern = "/\[$tag\][ ]*\b$find\b[ ]*\[\/$tag\]|$find/"; 
    $html = '<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>';
    
    $html = preg_replace_callback($pattern, function($match) use($find, $replace){
        if($match[0] == $find){
            return($replace);
        }else{
            return($match[0]);
        }
    }, $html);
    
    print $html;
    
    $pattern=“/\[no glossary\][]*\b此\b[]*\[\/no glossary\]]124;此/”;
    $html='不要替换[no glossary]这个[/no glossary]替换这个。

    ; $html=preg_replace_回调($pattern,'replace_function',$html); 函数替换函数($match){ 如果($match[0]==“this”){ 返回(“该”); }否则{ 返回($match[0]); } } 打印$html;

    动态:

    $pattern = "/\[no-glossary\][ ]*\bthis\b[ ]*\[\/no-glossary\]|this/"; 
    $html = '<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>';
    
    $html = preg_replace_callback($pattern, 'replace_function', $html);
    
    function replace_function($match){
        if($match[0] == 'this'){
            return('that');
        }else{
            return($match[0]);
        }
    }
    print $html;
    
    $tag = 'no-glossary';
    $find = 'this';
    $replace = 'that';
    
    $pattern = "/\[$tag\][ ]*\b$find\b[ ]*\[\/$tag\]|$find/"; 
    $html = '<p>Do not replace [no-glossary] this [/no-glossary] replace this.</p>';
    
    $html = preg_replace_callback($pattern, function($match) use($find, $replace){
        if($match[0] == $find){
            return($replace);
        }else{
            return($match[0]);
        }
    }, $html);
    
    print $html;
    
    $tag='no glossary';
    $find='this';
    $replace='that';
    $pattern=“/\[$tag\][]*\b$find\b[]*\[\/$tag\].$find/”;
    $html='不要替换[no glossary]这个[/no glossary]替换这个。

    ; $html=preg\u replace\u回调($pattern,function($match)use($find,$replace){ 如果($match[0]==$find){ 退货($更换); }否则{ 返回($match[0]); } },$html); 打印$html;
    感谢您为我编辑此内容。Brad。谢谢,但这导致了与上述问题相同的问题。请注意,当单词“this”前后都有空格时会出现问题,例如,“this”这对
    不起作用请不要替换此[/no glossary]替换此项。

    你好,Marcellus。此模式导致错误:警告:preg_replace()[function.preg replace]:eval()中未知的修饰符“n”(第6行/var/www/html/websites/sites/all/modules/contrib/devel/devel.module(1285):eval()'d代码)。请重新检查语法。@user2254788他忘记转义正斜杠:
    \b(这个)\b(?)((?:(?!\[no glossary\]))*?\[\/no glossary\])
    这与OP想要的正好相反。他不想替换介于
    [no glossary][/no glossary]
    之间的词。谢谢Mohammad,但它替换了错误的实例!你能尝试一个表达式来替换不在[no glossary]之间的词吗标签,但其他地方。@hamza dzcyberdev你明白了!有解决方案吗?@user2254788是的,我还在想,我想到了一个解决方案,但似乎PCRE RegEx引擎不支持它!所以我正在努力寻找解决办法!@user2254788看到更改答案哇。你一定是个天才!!我认为$1和$2是一个拼写错误,但它有效。这些placeholde吗对于目标词前后的空白空间,@ USER 22547 88是1美元和2美元,分别是第一个和第二个捕获组的内容,它们匹配空白空间(*+),如果<代码>不替换这个[ /没有词汇表],这将不能正常工作。< /代码>或代码>不替换[没有词汇表]。这取代了这个。
    @user2254788如果没关系,就用这个解决方案!再问一个快速(最后)问题。什么是严重的口音(`)在模式do?@user2254788中,他没有使用
    /
    作为定界符,而是使用
    `
    ,基本上你也可以使用
    ~
    等作为定界符!ps:这对
    不起作用,不要替换[no glossary]lol这个[/no glossary]替换这个。

    Hamza-这个效果很好。现在我有两个解决方案。你能评论一下上面的@Casimir et Hippolyte solution。哪个更快?