Finding tag through regex

by Php Ninza on August 11, 2008

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 = “<a\s[^>]*href=(\”??)([^\" >]*?)\\1[^>]*>(.*)<\/a>”;

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

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

{ 5 comments… read them below or Shout @ me! }

heidzir November 5, 2008 at 9:27 am

hi,

the code did not close the img tags properly

example:

Reply

matsolof June 15, 2009 at 8:19 pm

I reworked the code somewhat, and the img tag now closes properly. At least if you use my sample code.

I also enhanced the code somewhat. Firstly, I included the alt attribute. Secondly, the code now collects the result as a variable ($image), that you can use anywhere you like (below the variable $image, that is, of course).

I don’t know in what context the code is used, so I don’t know how useful my new version is. I’m also not that experienced in writing regular expressions, so I don’t fully understand the original code. It may include needed functionality that is gone in my code.

$subject = ”;
$pattern = ‘//’;
ob_start();
if(preg_match($pattern,$subject,$match)){
echo $match[0];}
$image = ob_get_clean();
echo $image;

/matsolof

PS: If you try my code and don’t see any image, it may be because the image is removed. I’m currently reworking my image pages to flash and remowing all png images.

Reply

matsolof June 15, 2009 at 8:35 pm

In my first attempt to comment on the code above, most of my code disappeared for som reason. I make another attempt with some characters represented by html entities

$subject = ‘Vedum & Uddevalla 2006‘;
$pattern = ‘/(.*)/’;
ob_start();
if(preg_match($pattern,$subject,$match)){
echo $match[0];}
$image = ob_get_clean();
echo $image;

/matsolof

Reply

dhiraj pandey July 28, 2009 at 10:31 am

article is great..!! please clear one thing in arguments passes, what should be in place of $subject…??

Reply

dhiraj pandey July 28, 2009 at 1:31 pm

article is great..!! please clear one thing in arguments passes, what should be in place of $subject…??

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: