Jump to content

get all links from a string


onlyican

Recommended Posts

Hi, I have a string which contains a mix of Content and anchor links

 

Example:

//Example
$strContent = 'I am some <a href="/content.htm">Content</a> here, but I could have <a href="/link.php">a Link</a> <a href="http://www.example.com">Followed by another</a> Link, Mix external and not.';

 

I basically want to programatically pull out all of the Anchor links, and have an array of such

Array('/content.htm', '/link.php', 'http://www.example.com');

 

Help me please. Got a brain freeze as its Friday Afternoon

Link to comment
https://forums.phpfreaks.com/topic/219203-get-all-links-from-a-string/
Share on other sites

<?php
$strContent = 'I am some <a href="/content.htm">Content</a> here, but I could have <a href="/link.php">a Link</a> <a href="http://www.example.com">Followed by another</a> Link, Mix external and not.';
preg_match_all('~href=("|\')(.*?)\1~', $strContent, $out);
print_r($out[2]);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.