Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

get all href values in string

Posted on 2009-05-11
5
Medium Priority
?
305 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 2000 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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 count occurrences of each item in an array.
Suggested Courses

916 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