Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,003 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,358 people online right now. Registration is fast and FREE... Join Now!




Extract links from a string

 
Reply to this topicStart new topic

Extract links from a string

BetaWar
21 Nov, 2006 - 05:16 PM
Post #1

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
Okay so I have a script that makes anchor links dynamically, but I now need a way to create the link to the anchor, I was thinking that the fastest way woyuld be to take the same code that has the anchors in it and simply extract everything other than the links. Then add the # to make then know their destination. But this isn't working out as well as I would have hoped. I am using preq_replace() and it works for the initial part, however on the second area I can't get it to simply post the links and not the whole text again.

Here is a portion of the code that this deals with. Please help:

CODE
$navall = $content;
$content = nl2br($content);

$content = preg_replace( "/\<!\>(.*?)\<\/!\>/ims", "<a name='\\1'></a>\\1", $content);


$navminus = preg_replace( "/\<!\>(.*?)\<\/!\>/ims", "<a href='#\\1'>\\1</a>", $navall);

$navminus = preg_replace( "/\(.*?)\<a href='#(.*?)\'\>(.*?)\<\/a\>\/ims", "<a \\1>\\2</a><br>", $navminus);

$nav= $navminus;

User is online!Profile CardPM
+Quote Post

snoj
RE: Extract Links From A String
21 Nov, 2006 - 05:27 PM
Post #2

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
I'm not quite understanding...could you provide some examples...like what it starts out as, what the out is suppose to be, and what really is being outputted?
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Extract Links From A String
21 Nov, 2006 - 09:34 PM
Post #3

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,304



Thanked: 101 times
Dream Kudos: 1275
My Contributions
Okay so if I had a string going in as:

CODE

<b>Hello this is <a href="http://www.site.com">Test</a>. I am happy to be here.</b>


I want it to output:

CODE
<a href="http://www.site.com">Test</a>


But it still outputs:

CODE
Hello this is <a href="http://www.site.com">Test</a>. I am happy to be here.

User is online!Profile CardPM
+Quote Post

psykoprogrammer
RE: Extract Links From A String
22 Nov, 2006 - 06:41 AM
Post #4

D.I.C Head
Group Icon

Joined: 9 Oct, 2006
Posts: 68


Dream Kudos: 50
My Contributions
Ok, here is a regex that will do what you want. However, it relies on the HTML being well formatted. It will fail on conditions where a less than sign is not properly converted to character codes.

CODE

// This is a valid XHTML string.
<b>Hello this is <a href="http://www.site.com">This is a &lt; test, m'k?</a>. I am happy to be here.</b>

// This one is INVALID and the regex fails
<b>Hello this is <a href="http://www.site.com">This is a < test, m'k?</a>. I am happy to be here.</b>


I built the regex in a little program I am writing that you may wanna check out (http://regstudio.adampresley.com - shameless plug smile.gif ). The regex is:

CODE

(<a[^<]+)(?=<\/a>)(<\/a>)


It is looking for anything starting with "<a", and continues capture till it finds "<". The next part tells the regex engine to only capture the previous expression if it finds "</a>" ahead in the expression. It then captures the "</a>".

Hope that helps a bit.

This post has been edited by psykoprogrammer: 22 Nov, 2006 - 06:42 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 08:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month