2

Lets say i had a string as in $str ::

$str = "Test,

Here\'s an interesting in-house litigation position with JPMorgan Chase in New York I thought you might be interested inL

<a href="http://www.example.com/lcdetail.php?akey=e1725">http://www.example.com/lcdetail.php?akey=e1725</a>

They are not using recruiters to fill this job, so if you are interested you can just apply directly.  

http://www.example.com/lcdetail.php?akey=e1725

Cordially
--Harrison";

Now i want to added tag to the links whom tag is not present.

I had used this function but not returning desired value.

function processString($s){  
  return preg_replace('@(?<!href=")(https?:\/\/[\w\-\.!~?&=+\*\'(),\/]+)((?!\<\/\a\>).)*@i','<a href="$1">$1</a>',$s);
}

My requirement is NOT to ADD anchor tag in link if it is present and ADD to those which haven't.

Thanks.

0

1 Answer 1

4

Well, I just modified your lookbehind path to (?<!href="|">) to avoid the link inside the tags...

So use :

function processString($s){  
  return preg_replace('@(?<!href="|">)(https?:\/\/[\w\-\.!~?&=+\*\'(),\/]+)((?!\<\/\a\>).)*@i','<a href="$1">$1</a>',$s);
}

Regex101 Demo

Codepad Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.