Convert URLs from text to links, even if no protocol

Let's say $ content is the content of the text area

/*Convert the http/https to link */ $content = preg_replace('!((https://|http://)+[a-z0-9_./?=&-]+)!i', '<a target="_blank" href="$1">$1</a> ', nl2br($_POST['helpcontent'])." "); /*Convert the www. to link prepending http://*/ $content = preg_replace('!((www\.)+[a-z0-9_./?=&-]+)!i', '<a target="_blank" href="http://$1">$1</a> ', $content." "); 

This worked fine for links, but realized that it breaks the markup when the image is inside the text ...

I'm trying it like this:

 $content = preg_replace('!\s((https?://|http://)+[a-z0-9_./?=&-]+)!i', ' <a href="$1">$1</a> ', nl2br($_POST['content'])." "); $content = preg_replace('!((www\.)+[a-z0-9_./?=&-]+)!i', '<a target="_blank" href="http://$1">$1</a> ', $content." "); 

Like images, but the problem is that url with the format http: // or https: // will no longer be converted ..:

google.com β†’ Not converted (as expected)

www.google.com β†’ Well Converted

http://google.com β†’ Not Converted (unexpectedly)

https://google.com β†’ Not Converted (unexpectedly)

What am I missing?

-Edit -

Current near-working solution:

 $content = preg_replace('!(\s|^)((https?://)+[a-z0-9_./?=&-]+)!i', ' <a href="$2" target="_blank">$2</a> ', nl2br($_POST['content'])." "); $content = preg_replace('!(\s|^)((www\.)+[a-z0-9_./?=&-]+)!i', '<a target="_blank" href="http://$2" target="_blank">$2</a> ', $content." "); 

The fact is that if this is an input:

www.funcook.com http://www.funcook.com https://www.funcook.com funcook.com http://funcook.com https://funcook.com

All the urls I want (all except name.domain) translate as expected, but this is the output

www.funcook.com http://www.funcook.com https://www.funcook.com; funcook.com http://funcook.com https://funcook.com

Pay attention to a; inserted, any idea why?

+6
source share
5 answers

try the following:

 preg_replace('!(\s|^)((https?://|www\.)+[a-z0-9_./?=&-]+)!i', ' <a href="$2">$2</a> ',$text); 

It picks up links starting with http: // or www.

Example

+8
source

You cannot 100%. Of course, there may be links such as stackoverflow.com that do not have www. .

If you only target these links:

 !(www\.\S+)!i 

It should work well enough for you.


EDIT: Regarding your new question, why the links to http are not converted, but https do, your first template only looks for https:// or http://. , what's wrong. Simplify it by replacing:

 (https://|http://\.) 

FROM

 (https?://) 

Which will make s optional.

+2
source

Another way to add hyperlinks is that you can take the text you want to parse for links and blow it into an array. Then pass it through foreach (a very fast function - http://www.phpbench.com/ ) and change everything that starts with http: // or https: //, or www., Or ends with .com / .org / etc to the link.

I think maybe something like this:

 $userTextArray = explode(" ",$userText); foreach( $userTextArray as &$word){ //if statements to test if if it starts with www. or ends with .com or whatever else //change $word so that it is a link } 

Your changes will be reflected in the array, since you have a "&". before $ userText in your foreach clause. Now just blow the array back to the string and you will go well.

It made sense in my head ... But I'm not 100% sure that this is what you are looking for

+1
source

I had a similar problem. Here is the function that helped me. Perhaps this will suit your needs:

 function clHost($Address) { $parseUrl = parse_url(trim($Address)); return str_replace ("www.","",trim(trim($parseUrl[host] ? $parseUrl[host].$parseUrl[path] : $parseUrl[path]),'/')); } 

This function will return the domain without the protocol and "www", so you can add them later.

For instance:

 $url = "http://www.". clHost($link); 

I did it this way because I did not find a good regular expression.

+1
source

\s((https?://|www.)+[a-z0-9_./?=&-]+)

The problem is that your \ s start causes the match to start with a space, so if you don't have this start space, your match fails. Reg exp is fine (without \ s), but to avoid replacing the images, you need to add something to avoid matching them.

If the images are pure html, use this: (?<!src=")((https?://|www.)+[a-z0-9_./?=&-]+)

This will look for src = "in front of the url to ignore it.

If you use a different mark, tell me and I will try to find another way to avoid the images.

0
source

Source: https://habr.com/ru/post/920731/


All Articles