Tag Archives: regex

Extract domain information from a URL with and without regex

At times Php developers need to parse domain information from a given url , Usually a $_SERVER[‘REQUEST_URI’] , The php function www.php.net/parse_url is used to extract domain name, Variables from a url string, The list of information extracted (Return values) from parse url are:-

  • scheme – e.g. http, https, etc.
  • host:- the domain name( www.google.com)
  • port:- the number of port used
  • user :- username if specified on domain url
  • pass :- password if specified on domain url
  • path :- path of the page after the url
  • query – after the question mark ?
  • fragment – after the hashmark #

Continue reading Extract domain information from a URL with and without regex

Finding tag through regex

I am saving this for my reference , A post with all commonly used regex, The regex like saving image tags, html links , email validation etc . are one of the most common used regex !

1)   Finding <img> tag through php using regex.

$pattern = ‘/<img[^>]+src[\s='”]’;
$pattern .= ‘+([^”‘>\s]+)/is’;

if(preg_match($pattern,$subject,$match))
{
echo $match[0];
}

2) Find all links in a html page

$input = file(“page.html”);

$regexp = “<as[^>]*href=(“??)([^” >]*?)\1[^>]*>(.*)</a>”;

if(preg_match_all(“/$regexp/siU”, $response, $matches))