I'm trying to get the value of the href
attribute of the first <a>
tag in a post which is an image.
This is what I have so far:
$pattern = "/<a.+href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\").*>/i";
$output = preg_match_all($pattern, $post->post_content, $matches);
$first_link = $matches[1][0];
However, this does not work.
I have a code to get the src
value of an <img>
tag which does work:
$pattern = "/<img.+src=[\'"]([^\'"]+)[\'"].*>/i";
$output = preg_match_all($pattern, $post->post_content, $matches);
$first_img = $matches[1][0];
As I'm no expert with regular expressions and php in general I have no idea what I'm doing wrong.
Also I couldn't find any decent, organized guide to regular expressions so a link to one could be useful as well!