Note that there are some explanatory texts on larger screens.

plurals
  1. POInsert space after semi-colon, unless it's part of an HTML entity
    text
    copied!<p>I'm trying to insert a space after each semi-colon, unless the semi-colon is part of an HTML entity. The examples here are short, but my strings can be quite long, with several semi-colons (or none).</p> <pre><code>Coca&amp;#8209;Cola =&gt; Coca&amp;#8209;Cola (&amp;#8209; is a non-breaking hyphen) Beverage;Food;Music =&gt; Beverage; Food; Music </code></pre> <p>I found the following regular expression that does the trick for short strings:</p> <pre><code>&lt;?php $a[] = 'Coca&amp;#8209;Cola'; $a[] = 'Beverage;Food;Music'; $regexp = '/(?:&amp;#?\w+;|[^;])+/'; foreach ($a as $str) { echo ltrim(preg_replace($regexp, ' $0', $str)).'&lt;br&gt;'; } ?&gt; </code></pre> <p>However, if the string is somewhat large, the <code>preg_replace</code> above actually crashes my Apache server (The connection to the server was reset while the page was loading.) Add the following to the sample code above:</p> <pre><code>$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.'; </code></pre> <p>The code above (with the large string) crashes Apache but works if I run PHP on the command line.</p> <p>Elsewhere in my program I use <code>preg_replace</code> on much larger strings without problem, so I'm guessing something it the regular expression overwhelms PHP/Apache.</p> <p>So, is there a way to 'fix' the regex so it works on Apache with large strings or is there another, safer, way to do this?</p> <p>I'm using PHP 5.2.17 with Apache 2.0.64 on Windows XP SP3, if it's any help. (Unfortunately, upgrading either PHP or Apache is not an option for now.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload