Forum Moderators: coopster

Message Too Old, No Replies

Striping data help needed

Remove all text between <a and a>?

         

IamStang

11:51 pm on Aug 14, 2005 (gmt 0)

10+ Year Member



Going to try to explain this as best I can.

I have a need to completely remove links from a large number of text documents. I know that I could simply use the "$strip_tags()" functon. However, I have been asked to remove the entire link, text and all. There are several links per document in random locations throughout each document.

So, how could I remove text from between <a and a> and then remove the leftover <aa>? (Or similar method) Is this even possible?

Any help is greatly appreciated.

mcibor

9:35 am on Aug 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use regex expresion, however I'm not very into them. I use strpos [php.net] and substr [php.net] instead.

while(($begin = strpos($text, "<a"))!== false) {
$end = strpos($text, "</a>", $begin) + 4;//to the end of >
$text = substr($text, 0, $begin) . substr($text, $end, strlen($text) - $end);
}

I'm not sure if you won't have to change $end to +5 or +3. Also to strlen you may need to add or remove 1.

This is a not a very fast method, but will work. Unless someone doesn't write any with ereg_replace then use it.

Best regards
Michal Cibor

mattx17

11:50 pm on Aug 15, 2005 (gmt 0)

10+ Year Member



If you want to go the regular expression route:


$Text = "This is <a href='index.php'>a link</a>, and this is <a href='test.php'>another link</a>";
echo preg_replace("#\s?<a.*?/a>#i","",$Text);

Should yield:

This is, and this is

IamStang

11:02 pm on Aug 16, 2005 (gmt 0)

10+ Year Member



Cool!
And thanks folks!

Is much simpler than the method I devised. LOL. But hey, I did manage to find a way. Has to say something for persistance anyway.

Thanks Again!