Posted By

thefrosty on 10/02/10


Tagged

wordpress pregreplace


Versions (?)

preg_replace WordPress content on post_save


 / Published in: PHP
 

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

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?

  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');

Report this snippet  

You need to login to post a comment.