Skip to content

Instantly share code, notes, and snippets.

@tillsanders
Last active August 29, 2015 14:23
Show Gist options
  • Save tillsanders/6f6670e88086ba646e91 to your computer and use it in GitHub Desktop.
Save tillsanders/6f6670e88086ba646e91 to your computer and use it in GitHub Desktop.
Useful regex stuff

Useful regex stuff I wrote

Make links clickable:

$re = "/((?:http:\/\/|www\.).*)(?:\s|$)/U";
$html = preg_replace($re, '<a href="$1" target="_blank">$1</a> ', $plain);

Filter empty paragraphs (Wysiwyg sucks. So does Wordpress.):

$re = "/<(?<tag>[a-z])>(\s|&nbsp;|<br\s*\/*>|<(?<tag2>[a-z])>(\s|&nbsp;|<br\s*\/*>)*<\/\k<tag2>>)*<\/\k<tag>>/";
$html = preg_replace($re, '', $plain);

Matches:

<p> </p>
<p>  </p>
<p></p>
<p>&nbsp;</p>
<p><br /></p>
<p><br/></p>
<p><br></p>
<p><b></b></p>
<p><b>&nbsp;</b></p>
<p>
	<b></b>
</p>
<p>
<b></b>
</p>
<p>
<b> </b>
</p>
<b></b>
<b>&nbsp;</b>
<p align="center"><b>&nbsp;</b></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment