在分号之后插入空格,除非它是HTML实体的一部分

我试图在每个分号之后插入空格,除非分号是HTML实体的一部分。 这里的例子很简短,但我的string可以很长,有几个分号(或没有)。

Coca‑Cola => Coca‑Cola (‑ is a non-breaking hyphen) Beverage;Food;Music => Beverage; Food; Music 

我发现下面的正则expression式可以做到短string的技巧:

 <?php $a[] = 'Coca‑Cola'; $a[] = 'Beverage;Food;Music'; $regexp = '/(?:&#?\w+;|[^;])+/'; foreach ($a as $str) { echo ltrim(preg_replace($regexp, ' $0', $str)).'<br>'; } ?> 

但是,如果string有点大,上面的preg_replace实际上会崩溃我的Apache服务器(在加载页面的时候,服务器的连接被重置)。将上面的代码添加到上面的示例代码中:

 $a[] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '. 'In blandit metus arcu. Fusce eu orci nulla, in interdum risus. '. 'Maecenas ut velit turpis, eu pretium libero. Integer molestie '. 'faucibus magna sagittis posuere. Morbi volutpat luctus turpis, '. 'in pretium augue pellentesque quis. Cras tempor, sem suscipit '. 'dapibus lacinia, dolor sapien ultrices est, eget laoreet nibh '. 'ligula at massa. Cum sociis natoque penatibus et magnis dis '. 'parturient montes, nascetur ridiculus mus. Phasellus nulla '. 'dolor, placerat non sem. Proin tempor tempus erat, facilisis '. 'euismod lectus pharetra vel. Etiam faucibus, lectus a '. 'scelerisque dignissim, odio turpis commodo massa, vitae '. 'tincidunt ante sapien non neque. Proin eleifend, lacus et '. 'luctus pellentesque;odio felis.'; 

上面的代码(与大string)崩溃的Apache,但工程,如果我在命令行上运行PHP。

在我的程序的其他地方,我使用preg_replace处理大得多的string而没有问题,所以我猜测正则expression式会压倒PHP / Apache。

那么,有没有办法“修复”正则expression式,所以它在大string的Apache上工作还是有另一种更安全的方法来做到这一点?

如果有任何帮助,我在Windows XP SP3上使用PHP 5.2.17和Apache 2.0.64。 (不幸的是,升级PHP或Apache目前不是一种select。)

我会建议这个匹配表达式:

 \b(?<!&)(?<!&#)\w+; 

…匹配一系列前面没有&符号(或符号后跟散列符号)但后面跟有分号的字符(字母,数字和下划线)。

它分解为:

 \b # assert that this is a word boundary (?<! # look behind and assert that you cannot match & # an ampersand ) # end lookbehind (?<! # look behind and assert that you cannot match &# # an ampersand followed by a hash symbol ) # end lookbehind \w+ # match one or more word characters ; # match a semicolon 

换成字符串'$0 '

让我知道如果这不适合你

当然,你也可以使用[a-zA-Z0-9]而不是\w来避免匹配分号,但是我不认为会给你带来麻烦

另外,您可能还需要转义哈希符号(因为这是正则表达式注释符号),如下所示:

 \b(?<!&)(?<!&\#)\w+; 

编辑不知道,但我猜,把字边界在开始将使它更有效率(因此不太可能崩溃你的服务器),所以我改变了在表达式和分解。 ..

编辑2 …和更多的信息,为什么你的表情可能会让你的服务器崩溃: 灾难性的回溯 – 我认为这适用(?)嗯….很好的信息

FINAL EDIT如果你只是想在分号之后加一个空格, 如果后面还没有空格 (例如在pellentesque;odio添加一个pellentesque;odio但是在pellentesque; odio );然后在下面添加一个额外的lookahead结束,这将防止额外的不必要的空间被添加:

 \b(?<!&)(?<!&\#)\w+;(?!\s) 

你可以使用负面的后视:

 preg_replace('/(?<=[^\d]);([^\s])/', '; \1', $text) 

没有测试,因为我手边没有电脑,但是这个或者它的一个小的变化应该工作。

有了这样的问题,回调可能会有所帮助。

 (&(?:[A-Za-z_:][\w:.-]*|\#(?:[0-9]+|x[0-9a-fA-F]+)))?; 

扩展

 ( # Capture buffer 1 & # Ampersand '&' (?: [A-Za-z_:][\w:.-]* # normal words | \# # OR, code '#' (?: [0-9]+ # decimal | x[0-9a-fA-F]+ # OR, hex 'x' ) ) )? # End capture buffer 1, optional ; # Semicolon ';' 

测试用例http://ideone.com/xYrpg

 <?php $line = ' Coca&#8209;Cola Beverage;Food;Music '; $line = preg_replace_callback( '/(&(?:[A-Za-z_:][\w:.-]*|\#(?:[0-9]+|x[0-9a-fA-F]+)))?;/', create_function( '$matches', 'if ($matches[1]) return $matches[0]; return $matches[0]." ";' ), $line ); echo $line; ?>