Solved

get all href values in string

Posted on 2009-05-11
5
295 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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