Find URLs in Text description With Regex PHP

Saturday, 20. October 2012

This little regex script gives you another simple way to fetch for all possible URLs embedded as simple text in a your rather large description text.

It’s flexible and you can add more filters appropriate for your requirement as you wish..

# take it to the test!
$matches = null;
$returnValue = preg_match_all('%(https://|http://|www)[a-zA-Z0-9\/#&-_\.]+%', $desc_variable, $matches);
# see what you got!
var_dump($matches);

Enjoy!

Remove Everything After Character Pattern Preg_Replace Regex expression

Monday, 23. July 2012

A little snippet that simply removes everything after a matched pattern using preg_replace regular expression.

Example

# remove everything after TEL: symbol
$address = "John Scott, Leeds, UK. TEL: 000111-234 345";
# new address
$new_address = preg_replace('/TEL:.*/i', '', $address, -1, $count);

Enjoy!