Suppose someone enters a string in a textarea
like this
"The best search engine is www.google.com."
or maybe
"The best search engine is https://www.google.co.in/?gfe_rd=cr&ei=FLB1U4HHG6aJ8Qfc1YHIBA."
Then i want to highlight the link as stackoverflow does.
And also i want to file_get_contents
to get one image , a short description and title of the page.
Most probably i wanna check if the string contains a url or not -> two times.
- On keyup of
textarea
using jQuery and therefore using theget_file_contents
- When the string is recieved by php.
Possibly how can i do this?
UPDATE
function parseHyperlinks($text) {
// The Regular Expression filter
$reg_exUrl1 = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$reg_exUrl2 = "/[\w\d\.]+\.(com|org|ca|net|uk)/";
// The Text you want to filter for urls
// Check if there is a url in the text
if(preg_match($reg_exUrl1, $text, $url)) {
// make the urls hyper links
return preg_replace($reg_exUrl1, "<a class=\"content-link link\" href=\"{$url[0]}\">{$url[0]}</a> ", $text);
} else if(preg_match($reg_exUrl2, $text, $url)){
return preg_replace($reg_exUrl2, "<a class=\"content-link link\" href=\"{$url[0]}\">{$url[0]}</a> ", $text);
}else{
// if no urls in the text just return the text
return $text;
}
}
- This works only if
$str='www.google.com is the best'
or$str='http://www.google.com is best'
but not if$str='http://stackoverflow.com/ and www.google.com is the best'
First off you create the html then you need to an AJAX to request to the server. Consider this sample codes:
HTML/jQuery:
And on your php that will process, in this case (index.php):