Skip to content

Instantly share code, notes, and snippets.

@wolffe
Created July 8, 2013 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolffe/5949873 to your computer and use it in GitHub Desktop.
Save wolffe/5949873 to your computer and use it in GitHub Desktop.
This function emulates WordPress wpautop() function for custom PHP scripts, parses URL addresses and adds smilies/emoticons.
<?php
function smilieMe($text) {
$smiliesFind = array(
'/:\)/',
'/:P/',
'/:D/',
'/:S/',
'/:\(/',
'/:8/',
'/:tea/',
'/:o/',
'/:O/',
'/:q/',
'/:Q/',
'/:hug/',
'/:joy/',
'/:yes/',
'/:no/',
);
$smiliesReplace = array(
'<img src="images/smilies/smile.png" alt=":)" title=":)" />',
'<img src="images/smilies/tongue.png" alt=":P" title=":P" />',
'<img src="images/smilies/grin.png" alt=":D" title=":D" />',
'<img src="images/smilies/confused.png" alt=":S" title=":S" />',
'<img src="images/smilies/sad.png" alt=":(" title=":(" />',
'<img src="images/smilies/proud.png" alt=":8" title=":8" />',
'<img src="images/smilies/tea.gif" alt=":tea" title=":tea" />',
'<img src="images/smilies/wow.png" alt=":o" title=":o" />',
'<img src="images/smilies/wow.png" alt=":O" title=":O" />',
'<img src="images/smilies/yay.png" alt=":q" title=":q" />',
'<img src="images/smilies/yay.png" alt=":Q" title=":Q" />',
'<img src="images/smilies/hug.gif" alt=":hug" title=":hug" />',
'<img src="images/smilies/joy.gif" alt=":joy" title=":joy" />',
'<img src="images/smilies/yes.gif" alt=":yes" title=":yes" />',
'<img src="images/smilies/no.gif" alt=":no" title=":no" />',
);
return preg_replace($smiliesFind, $smiliesReplace, $text);
}
function roo_autop($content) {
$content = trim($content);
$content = stripslashes($content);
/*
* temporarily replace two or more consecutive newlines
* into SOH characters (Start of Heading - first character of a message header)
*/
$content = preg_replace('~(\r\n|\n){2,}|$~', "\001", $content);
// convert remaining single newlines into HTML <br>
$content = nl2br($content);
// replace SOH characters with paragraphs
$content = preg_replace('/(.*?)\001/s', "<p>$1</p>\n", $content);
$content = smilieMe($content);
// parse URL addresses (ftp, http, https)
$content = preg_replace('*(f|ht)tps?://[A-Za-z0-9\./?=\+&%]+*', '<a href="$0">$0</a>', $content);
return $content;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment