-2

My string: some text some text < b >some text< /b > some text < a href = http ://sometextsometext< b >sometext< /b >sometextsometext >text< /a > some text some text

Is there any way using preg_match or str_replace to only remove the < b >< /b > in the link tag?

Thanks

Richard
  • 3
  • 3
  • please use a real example. what are you *actually* trying to do? – Mike 'Pomax' Kamermans Nov 10 '14 at 05:46
  • Hi, Richard can you add a clear example that what exactly you link is and what exactly you want to remove, instead of a series of some text – Ashish Awasthi Nov 10 '14 at 05:48
  • And please state, if you want to delete the link tag altogether or leave the link text but remove the actual anchor functionality. – Paul Nov 10 '14 at 05:50
  • possible duplicate of [How to remove a link from content in php?](http://stackoverflow.com/questions/3830717/how-to-remove-a-link-from-content-in-php) – Paul Nov 10 '14 at 05:50
  • JCPP0651, PP0651, BNC Male CRIMP Plug For RG59 Suits our Belden coax- 0.9mm centre pin....
    Maybe more info here
    – Richard Nov 10 '14 at 06:00
  • JCPP0651, PP0651, BNC Male CRIMP Plug For RG59 Suits our Belden coax- 0.9mm centre pin....
    Maybe more info here
    – Richard Nov 10 '14 at 06:01
  • thanks everyone for your prompt comms...I don't want to remove the link, I have a string which contains text with the "bold" html tags and I have the "bold" html tags in the link tag within the string. – Richard Nov 10 '14 at 06:04
  • All the bold tags in the string got created using str_replace in order to highlight search terms. This will however break the link tag if words are found within the link tag so I'm trying to only remove the bold tags within the link tag without removing the link tag – Richard Nov 10 '14 at 06:06

3 Answers3

1

Okay, so here's an idea using PHP's function preg_replace_callback

<?php


// SET TEXT TO BE USED
$string = 'some text some text <b>some text</b> some text <a href=http://sometextsometext<b>sometext</b>sometextsometext>text</a> some text some text.  And We Have A <a href=http://google.com>Google</a> Link';


// USE A CALLBACK FUNTION TO SCAN THROUGH LINKS
$string = preg_replace_callback('~<a.*?</a>~', 'remove_crap_from_links', $string);

print $string;


// THIS IS THE CALLBACK FUNCTION ... EACH LINK IS STORED AS '$m'
function remove_crap_from_links($m) {

    // PULL OUT THE PART OF THE LINK BEFORE THE CLOSING LINK BRACKET
    // (USE A NEGATIVE LOOKAHEAD TO MAKE SURE THAT IT CAN'T HAVE ANY OPENING/CLOSING HTML BRACKETS IN THERE
    if (preg_match('~<a(.*?)>(?:[^<>]*?)</a>~i', $m[0], $url_matches)) {

        // RUN A PHP strip_tags FUNCTION TO PULL OUT ANY HTML TAGS FOUND IN THE LINK BODY
        $stripped_url = strip_tags($url_matches[1]);

        // REBUILD THE URL, USING THE $stripped_url IN PLACE OF WHAT WAS ALREADY THERE
        $clean_url = preg_replace('~(<a)(.*?)(>(?:[^<>]*?)</a>)~', '$1'.$stripped_url.'$3', $m[0]);

    }


    return $clean_url;


}

So basically, I'm taking the part that has been suggested a couple of times with PHP's strip_tags function, but only using on parts that it finds inside of link tags.

Here is a working demo

Quixrick
  • 3,190
  • 1
  • 14
  • 17
0

You can use strip_tags function in php to remove html tags.

$text = '

Test paragraph.

Other text'; echo strip_tags($text);

Output : Test paragraph. Other text

  • Thanks...however I don't want to remove the link tag...I just want to remove the bold tag within the link tag but keep the bold tags outside of the link tag – Richard Nov 10 '14 at 06:52
0

strip_tags - Strip HTML and PHP tags from a string

PHP Code

<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>

The above example will output:

Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>

Ref: http://php.net/manual/en/function.strip-tags.php

DJ MHA
  • 598
  • 3
  • 18
  • Thanks...however I don't want to remove the link tag...I just want to remove the bold tag within the link tag but keep the bold tags outside of the link tag – Richard Nov 10 '14 at 06:52