Solved

get all href values in string

Posted on 2009-05-11
5
293 Views
Last Modified: 2012-05-06
i have this string:

<p>this is a string with one <a href="http://www.velodays.com/" title="">link</a> here and another <a href="http://www.seandelaney.co.uk/" title="">link</a> here.</p>

I want to remove all <a></a> tags but keep the href values.

So for example the above would output as:

<p>this is a string with one http://www.velodays.com/ here and another http://www.seandelaney.co.uk/ here.</p>

I tried using strpos and substr but couldnt manage a solution.

Can somebody please correct me and help me get a working solution ?
0
Comment
Question by:ellandrd
  • 3
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24357349
This regexp might help:
$str = '<p>this is a string with one <a href="http://www.velodays.com/" title="">link</a> here and another <a href="http://www.seandelaney.co.uk/" title="">link</a> here.</p>';
$str = preg_replace('@<a\s+href="([^"]+)"[^>]*>[^<>]*</a>@i','$1',$str);
echo $str;

Open in new window

0
 
LVL 16

Author Comment

by:ellandrd
ID: 24362896
that works fine but how do i make it work if the link format is:

<a title="" href=""></a>
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 500 total points
ID: 24362961
Try this regexp then:
$str = preg_replace('@<a\s+.*href="(.*)".*>.*</a>@iU','$1',$str);

Open in new window

0
 
LVL 16

Author Closing Comment

by:ellandrd
ID: 31580243
thank you - both work perfect!
0
 
LVL 16

Author Comment

by:ellandrd
ID: 24363743
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question