Friday, 8 October 2010

How to remove hyperlink from a string using php?

<?php

function findAndReplace($arr) {
    return '<strong>'.$arr[1].'</strong>';
}

$inputText = 'Why use <a href="#"  style="padding-left:5px"> PHP </a>?';
echo "<br />Input is: <br />";
echo $inputText;

$returnText = preg_replace_callback('@<a(?:.*)>(.*)</a>@isU', 'findAndReplace', $inputText);

echo "<br /><br />Output will be: <br />";
echo $returnText;

?>

Input is:
Why use PHP ?

Output will be:
Why use PHP ?

4 comments:

  1. hi - this is an awesome function and Im looking for something similar to replace spaces in my urls - can this be done using a call back function? how would one do it?

    ReplyDelete
  2. strip_tags($inputText);

    is easier than this.

    ReplyDelete