Solved

get all href values in string

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now