Kailash Yadav

Just another script samurai !!

Regular expression for HTML tags in string

Here is one of most powerful and used regular expression which was used by every developer once in their life for sure.

How to select content or string inside a HTML tag using regular expression in all languages.

I am sharing this regular expression with you. This regular expression is useful when you fetching content from Other Website using cURL.
]*>(.*?)} ?>

Here is one small example:
//Example to get text inside given tag
$content = file_get_contents("http://www.example.com");
$tag= 'h1'; //we will extract page heading

preg_match("{<'.$tag.'[^>]*>(.*?)}", $content, $match);
$data = $match[0];
echo $data;
?>

Note: This only returns text(string or HTML) inside the Tag you have given. It doesn’t include that tag.

posted by Kailash Yadav in Javascript,PHP and have No Comments