0

I've created a function that automatically detects links. It works quite fine, except when a HTML tag begins just after a link (without space).

Example :

http://www.google.com is cool

becomes

<a href="http://www.google.com" target="_blank">www.google.com</a> is cool

but

http://www.google.com<br />is cool

(without space after http:// in real) becomes

<a href="http://www.google.com<br /" target="_blank">www.google.com<br /</a> is cool

My regular expression doesn't stop at first <, though < is not allowed...

How can I fix that ? Or how can I exclude < efficiently ?

Here's my function :

function detect_linkg($str){        
    return preg_replace('#(https?://)([\w\d.&:\#@%/;$~_?\+-=]*)#','<a href="$1$2" target="_blank">$2</a>',$str);
}

Thanks for your help !

1 Answer 1

1

I think the problem is here: +-=

This actually creates a range, like a-z, but in this case from + to =, and if you look at the ascii chart you see that that includes < (but not >).

Simply escape - if you want the literal: +\-=

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

1 Comment

Thanks, that's the solution !

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.