Php search url in text

The basic function of this is to find any URLs in the block of text and turn them into hyperlinks. It will only find URLs if they are properly formatted, meaning they have a http, https, ftp or ftps. Check out the comments below for more solutions.

Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.

Comments

I want to thank you from the bottom of my little heart, I have been in search for this exact script for the past 6 months! Thank you, thank you, thank you. I would like to point out however that there was error but nothing I could not fix 😀 Assid from your code this is the code that I got to work for myself.

Hi All, Good one finding URL in text and making it as link. I have one doubt. Lets assume i have some text which contain plain URL and image tag with src(“https://www.something.com/images/test/jpeg”). So i need to skip img tag and only hyperlink URL. How is this possible?

How can i filter replace string for avoid image file links in text? som text with link https://hola.com and with image I want only replace link to web, and not replace link to image.

/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z](\/\S*)?/g

Find the Image URL using preg_match() syntax or something similar to extract JPG or PNG or GIF URLs from a mixed text and put them in an array or at last store the first url. maybe some syntax which searchs for strings that are beginning with http and ending with jpg/png/gif.. i believe it can be done with preg_match() Note: the text can be like that : $string =blablablabla”http://www.xxx.com/xxx.jpg”blablablabla $matches = array();
preg_match_all(‘!http://.+\.(?:jpe?g|png|gif)!Ui’ , $string , $matches);

public function formatUrlsInText($text)< $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z](\/\S*)?/"; preg_match_all($reg_exUrl, $text, $matches); $usedPatterns = array(); foreach($matches[0] as $pattern)< if(!array_key_exists($pattern, $usedPatterns))< $usedPatterns[$pattern]=true; $text = str_replace ($pattern, "" rel="nofollow"> ", $text); > > return $text; >

This is my version based on previous examples… hope this helps someone trying to force links on www without the http… simply call the function as follows: $body = txt2link($body);

function txt2link($text)< // force http: on www. $text = ereg_replace( "www\.", "http://www.", $text ); // eliminate duplicates after force $text = ereg_replace( "http://http://www\.", "http://www.", $text ); $text = ereg_replace( "https://http://www\.", "https://www.", $text ); // The Regular Expression filter $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z](\/\S*)?/"; // Check if there is a url in the text if(preg_match($reg_exUrl, $text, $url)) < // make the urls hyper links $text = preg_replace($reg_exUrl, ''.$url[0].'', $text); > // if no urls in the text just return the text return ($text); >

When using the above codes inside a loop (wherein bulleted lists are created that have text/links in them) I get a server error if there’s more than one list item with a url that needs to be converted. Any way to do this in a foreach? Thanks!

Читайте также:  Метод главных компонент python пример

Cases that this won’t catch:
http://localhost/test
http://1.2.3.4/test Also if you feed in
“http://www.google.co.uk/page is on the website http://www.google.co.uk/”
you will get some very mangled output, as when you search for “http://www.google.co.uk/” you will also match the text in the middle of the existing link for “http://www.google.co.uk/page”

your site is amazing. no bull shit. all good stuff. pls include my email id in ur permanent mailing list.

can that original script for finding urls be modifed to look for links ending with an .mp3 exstension?

Another equivalent function that make better results : http://code.seebz.net/p/autolink-php/ There is the same in javascript : http://code.seebz.net/p/autolink-js/

I’ve changed it a little bit, cause it won’t work for more than one url. Besides THANK YOU very much for the help.

public static function url_to_link($text) < // The Regular Expression filter $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z](\/\S*)?/"; // Check if there is a url in the text if (preg_match_all($reg_exUrl, $text, $url)) < // make the urls hyper links foreach($url[0] as $v)< //current position of the searached url $curpos = strpos($text,' '.$v)+1; //delete the url $text = substr_replace($text,'', $curpos, strlen($v)); //insert the link $text = substr_replace($text,''.$v.'', $curpos ,0); > return $text; > else < // if no urls in the text just return the text return $text; >>

is there a demo of this? i’ve been trying to put a url in a class on a div that will make the text in the div a link WIthout having to use the in the html markup. Is this overkill on unobtrusive css?

Hi guys This way is much easier. The cost converts URLs in $Text to html hyper links: if (preg_match(“/http/”, “$Text”) OR preg_match(“/www/”, “$Text”))
$ExplodeText = explode(” “, $Text);
foreach($ExplodeText as $Check)
>
>

Apologies, there was a mistake in the code I post above. Below is the corrected version: if (preg_match(“/http/”, “$Text”) OR preg_match(“/www/”, “$Text”))
$Text = str_replace(” www” , ” http://www” , $Text);
$Explode = explode(” “, $Text);
foreach($Explode as $Check) >
>

please list my email id in your mailing lists i would like to be notified about all the stuffs discussed here thanks!

Hi, If the url has ‘%20’ how can I make it? Example: http://www.lalala.com/%20text%20some.html Can you help me? Thank you Regards

Can some on post or email me the COMPLETE SCRIPT FINALLY WHICH CAN FIND ALL URLS IN A PAGE AND HIGHLIGHT THEM AS LINKS I am requesting as i do not know coding for my self as i am not into web-designing

This is great article but it can’t handle certain situation so i modified it bit so check the article below that works great with possible all situation… http://www.javaquery.com/2012/06/turn-urls-into-links-in-text-with.html

I have a chat log i want to get all links out of. How do i use this code to do this? *I am not a programmer* can i run some kind of batch command file or something and point to the text log?

 function linkToAnchor($text) < // The Regular Expression filter $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z](\/\S*)?/"; // The Text you want to filter for urls // Check if there is a url in the text if(preg_match_all($reg_exUrl, $text, $url)) < // make the urls hyper links $matches = array_unique($url[0]); foreach($matches as $match) < $replacement = ""; $text = str_replace($match,$replacement,$text); > return nl2br($text); > else < // if no urls in the text just return the text return nl2br($text); >> 

This worked for me, but won’t work without http:// or https://, how would I add so it would work with www?

Hello to every single one, it’s really a fastidious for me to visit this web site, it contains useful Information.

(\/\S*)?/"; // The Text you want to filter for urls $text = "The text you want to filter goes here. http://google.com"; // Check if there is a url in the text if(preg_match($reg_exUrl, $text, $url)) < // make the urls hyper links echo preg_replace($reg_exUrl, "", $text); > else < // if no urls in the text just return the text echo $text; >?> 

//easy—>worked for me $data =”my text http://www.facebook.com/c.hodari90“;
$data = preg_replace( ‘/(http|ftp)+(s)?:(\/\/)((\w|.)+)(\/)?(\S+)?/i’, ‘\4‘, $data ); echo $data; output = my text facebook.com/c.hodari90

Proposed code has a slight flaw. If $text contains two similar URLs, like “The text you want to filter has two URLs namely http://google.com but also http://google.com/index.html#hashtag” with both URLs starting the same, then the #hashtag in the second URL gets lost.
Since URLs inside a string are separated by spaces (I believe this is always the case), the following code works for me:

// The Regular Expression filter $reg_exUrl = "/(https?|ftps?|file):\/\/([a-z0-9]([a-z0-9_-]*[a-z0-9])?\.)+[a-z]\/?([a-z0-9\?\._-~&#=+%]*)?/i"; // The Text you want to filter for URLs $text = "The text you want to filter has two URLs namely http://google.com but also http://google.com/index.html#hashtag"; // add leading and trailing spaces they serve as URL delimiters in case // the URL is at the very beginning or end of $text $text = " ".$text." "; // Check if there is a url in the text if(preg_match_all($reg_exUrl, $text, $url)) < // make the URL hyper links $matches = array_unique($url[0]); foreach($matches as $match) < $replacement = "".$match.""; $text = str_replace(" ".$match." ", " ".$replacement." ", $text); > // if URLs in the text, return the text after // removing the leading and trailing spaces $text = trim(nl2br($text), " "); > else < // if no URLs in the text, return the original text after // removing the leading and trailing spaces echo trim($text, " "); >

By the way, I use a different regex which does not match URLs that contain forbidden characters like @ Anf finally… what a pain in the back to get this comment displayed the right way (I hope…).

Your code worked, but you forgot to close the tag!
I changed
$replacement = ““.$match.””;
to
$replacement = ““.$match.”“; Your code doesn’t print the text if there is an url present, therefor changed this:
// if URLs in the text, return the text after
// removing the leading and trailing spaces
$text = trim(nl2br($text), ” “);
to
// if URLs in the text, return the text after
// removing the leading and trailing spaces
$text = trim(nl2br($text), ” “);
echo $text; Hope this helps someone! 🙂

Источник

Extract URLs from text in PHP

How can I extract the link into another variable? I know it should be by using regular expression especially preg_match() but I don’t know how?

@ Michael Berkowski how it will be duplicate the user asked on May 26 ’09 at 14:13 but link mentioned by you asked on Dec 8 ’10 at 17:44. May be the reverse may true.

14 Answers 14

Probably the safest way is using code snippets from WordPress. Download the latest one (currently 3.1.1) and see wp-includes/formatting.php. There’s a function named make_clickable which has plain text for param and returns formatted string. You can grab codes for extracting URLs. It’s pretty complex though.

This one line regex might be helpful.

preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match); 

But this regex still can’t remove some malformed URLs (ex. http://google:ha.ckers.org ).

I had a play around with the WordPress formatting.php and using make_clickable is a nice idea but it ends up sucking in half of wordpress in dependencies.

This regex will match google:ha.ckers.org «@https?:\/\/(www\.)?[-a-zA-Z0-9\@:%._\+~#=]\.[a-zA-Z0-9()]\b([-a-zA-Z0-9()\@:%_\+.~#?&//=]*)@»; Dont remeber where i found it so can’t give credit

I tried to do as Nobu said, using WordPress, but to much dependencies to other WordPress functions I instead opted to use Nobu’s regular expression for preg_match_all() and turned it into a function, using preg_replace_callback() ; a function which now replaces all links in a text with clickable links. It uses anonymous functions so you’ll need PHP 5.3 or you may rewrite the code to use an ordinary function instead.

]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#'; return preg_replace_callback($regex, function ($matches) < return "\'> "; >, $text); > 

Just a note: I’ve updated your answer to use a anonymous function as a callback instead of using create_function() .

URLs have a quite complex definition — you must decide what you want to capture first. A simple example capturing anything starting with http:// and https:// could be:

preg_match_all('!https?://\S+!', $string, $matches); $all_urls = $matches[0]; 

Note that this is very basic and could capture invalid URLs. I would recommend catching up on POSIX and PHP regular expressions for more complex things.

The code that worked for me (especially if you have several links in your $string ):

$string = "this is my friend's website https://www.example.com I think it is cool, but this one is cooler https://www.stackoverflow.com :)"; $regex = '/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$. ;]*[A-Z0-9+&@#\/%=~_|$]/i'; preg_match_all($regex, $string, $matches); $urls = $matches[0]; // go over all links foreach($urls as $url) < echo $url.'
'; >

Hope that helps others as well.

If the text you extract the URLs from is user-submitted and you’re going to display the result as links anywhere, you have to be very, VERY careful to avoid XSS vulnerabilities, most prominently «javascript:» protocol URLs, but also malformed URLs that might trick your regexp and/or the displaying browser into executing them as Javascript URLs. At the very least, you should accept only URLs that start with «http», «https» or «ftp».

There’s also a blog entry by Jeff where he describes some other problems with extracting URLs.

preg_match_all('/[a-z]+:\/\/\S+/', $string, $matches); 

This is an easy way that’d work for a lot of cases, not all. All the matches are put in $matches. Note that this do not cover links in anchor elements (

It’s not stated what he’d use it for, hence I don’t account for that. He just wanted to get URLs into variables.

@Michael: Finding javascript URLs is not yet a vulnerability; using them without any check is. Sometimes the presence and number of such URLs is useful information. I’d have chosen a different delimiter. 🙂

Источник

PHP: How to find URLs in text (string) and display as links

Recently I was developing a custom solution for one of my client’s projects and I encountered a problem: A message (string) was stored in the database which may contain urls. I had to display that message and if it contains urls – it had to be display as active, clickable links. So here’s what worked for me:

PHP Snippet: Find URLs in string and make links using a function

$yourTextWithLinks = 'Here is an example for a text (string) that contains one or more url. Just visit http://www.google.com/ or http://google.com and this is the end of the example.'; $text = strip_tags($yourTextWithLinks); function displayTextWithLinks($s) < return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '$1', $s); > $text = displayTextWithLinks($text); echo $text;

Normally if you echo the variable $yourTextWithLinks it will display plain text with no active links. So now you can pass your string to the displayTextWithLinks() function and will return a text with clickable links in it.

If you’d like to use it without a function, here’s how.

Our reader Eugen found that the above solution is not working with urls containing non latin characters and found the following library:

Url highlight

Url highlight is a PHP library to parse URLs from string input. Works with complex URLs, edge cases and encoded input.

Url highlight features:

  • Replace URLs in string by HTML tags (make clickable)
  • Match URLs without scheme by top-level domain
  • Work with HTML entities encoded input
  • Extract URLs from string
  • Check if string is URL

Источник

Оцените статью