-2

I am trying to get a link from a preg match but it is not giving any output

Here is pattern:

    <a href="http://media1.xyz.com/full5/Animals/Rabbits/rabbits-16a.jpg" download="rabbits-16a"><img onmouseover="showFullScreen('inline')" onmouseout="showFullScreen('none')" src="http://media1.xyz.com/full5/Animals/Rabbits/rabbits-16a.jpg" id="wall" border="0" align="middle"  width="1920" height="1080"  alt="Rabbits 1920x1080 Wallpaper # 17" title="Rabbits HD Wallpaper #17" /></a>

I have tried this:

     preg_match("/\<a href=/\"(.*)\">/",$str,$title);

I want to get the link of href only.

chetna123
  • 47
  • 4

2 Answers2

-1

Using this match pattern should work

'/href=\"(.*?)\"/s'

I use it for matching the source in image tags I'm parsing from IMAP emails (in its original form '/src=\"(.*?)\"/s' But just changing src to href will match for you

Dave
  • 3,280
  • 2
  • 22
  • 40
-1
$input = '<a href="http://media1.xyz.com/full5/Animals/Rabbits/rabbits-16a.jpg" download="rabbits-16a"><img onmouseover="showFullScreen(\'inline\')" onmouseout="showFullScreen(\'none\')" src="http://media1.xyz.com/full5/Animals/Rabbits/rabbits-16a.jpg" id="wall" border="0" align="middle"  width="1920" height="1080"  alt="Rabbits 1920x1080 Wallpaper # 17" title="Rabbits HD Wallpaper #17" /></a>';

preg_match('/href=\"(.+?)\"/',$input,$title);

echo $title[1];

Which will output:

http://media1.xyz.com/full5/Animals/Rabbits/rabbits-16a.jpg

http://3v4l.org/QnJ9e

Ben Fortune
  • 31,623
  • 10
  • 79
  • 80