preg_replace WordPress content on post_save


/ Published in: PHP
Save to your folder(s)

I\'m definitely not the best at Regex. But this function seems to be timing out, and adding like 4o links to the post. Got any ideas?


Copy this code and paste it in your HTML
  1. function replaceContent($content = '') {
  2.  
  3. /* Process names */
  4. $content = preg_replace("~@(\w+)~", "<a href=\"http://www.twitter.com/\\1\" class=\"twitter-anywhere-user\">@\\1</a>", $content);
  5. $content = preg_replace("~^(\w+):~", "<a href=\"http://www.twitter.com/\\1\" class=\"twitter-anywhere-user\">@\\1</a>:", $content);
  6.  
  7. /* Process hashtags */
  8. $content = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $content);
  9.  
  10. /* Process links */
  11. $content = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" rel=\"external nofollow\">\\2</a>", $content);
  12. $content = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" rel=\"external nofollow\">\\2</a>", $content);
  13.  
  14. return $content;
  15. }
  16.  
  17. add_action('save_post', 'replaceContent');

URL: http://themelit.com/themes/tweets/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.