SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

Regex for Extracting URLs in Plain Text

       

Here is a Regex for extracting URLs from text. However, these links will not already be hyperlinked or source attribtues from images or iframes.

This example is in PHP. I was trying to format a Wordpress page to auto hyperlink but preserve embeded images, iframes, etc.

Wordpress does some funky stuff w/ regard to formatting, which is why I ended up going with the nl2br function and then removing double instances.

A little hacky but it seems to work.

<?php
  $regex = '~^http(.*?)(?=[\<])~im';
  $content = nl2br(get_the_content());
  $content = preg_replace($regex, '<a href="$0" target="_blank">$0</a>', $content);

  $html = nl2br(html_entity_decode(esc_html($content)));
  $html = str_replace('<br /><br />', '<br/>', $html);
  echo $html;
Tagged w/ #php #regex #wordpress

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app