0

I have a regex that extracts typed URL's from a string (a description text that could include typed URL's) and converts them to href's. This all works fine except for the fact that when a URL is typed and it's last character is a "," of ")" it also takes this character as part of the URL. How could I prevent this?

Example text:

Hi this is my beautiful message which contains a link (see www.website.com) and some more info.

My regex reads the URL but also takes the last character ")" when it creates the href, resulting in a bad link.

My Regex:

preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);

2 Answers 2

1

It looks like your matching anything after the url.try this.

preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ][^\,)]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);
Sign up to request clarification or add additional context in comments.

Comments

1

Include "," and ")" in the list of URL terminating characters. Don't forget to escape ")" with a backslash. In other words, try:

preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< \),]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $text);

(I haven't tested this.)

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.