Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To begin with, <strong><a href="http://php.net/manual/en/function.str-replace.php" rel="nofollow noreferrer"><code>str_replace()</code></a></strong> (which you referenced in your original question) is used to find a literal string and replace it. <strong><a href="http://php.net/manual/en/function.preg-replace.php" rel="nofollow noreferrer"><code>preg_replace()</code></a></strong> is used to find something that matches a regular expression and replace it.</p> <p>In the following code sample I use <code>\s+</code> to find one or more occurrences of white space (new line, tab, space...). <a href="http://www.regular-expressions.info/reference.html" rel="nofollow noreferrer"><strong><code>\s</code> is whitespace</strong></a>, and the <code>+</code> modifier means one or more of the previous thing.</p> <pre><code>&lt;?php // Test string with white space and line breaks between paragraphs $var = "&lt;p&gt;some text&lt;/p&gt; &lt;p&gt;another text&lt;/p&gt; &lt;p&gt;more text&lt;/p&gt;"; // Regex - Use ! as end holders, so that you don't have to escape the // forward slash in '&lt;/p&gt;'. This regex looks for an end P then one or more (+) // whitespaces, then a begin P. i refers to case insensitive search. $search = '!&lt;/p&gt;\s+&lt;p&gt;!i'; // We replace the matched regex with an end P followed by a begin P w no // whitespace in between. $replace = '&lt;/p&gt;&lt;p&gt;'; // echo to test or use '=' to store the results in a variable. // preg_replace returns a string in this case. echo preg_replace($search, $replace, $var); ?&gt; </code></pre> <p><a href="http://codepad.org/Ka4h651Y" rel="nofollow noreferrer"><strong>Live Example</strong></a></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