This tutorial will provide example of how to find url in text and display as link in php. This post will give you simple example of find urls in content and make link using PHP. This article will give you simple example of php find url in text and make link. This tutorial will give you simple example of php find url in string and make link. Follow bellow tutorial step of find url in string and mke link in php.
Here, i will give you very simple example, to find url from string and make anchor tag link using php. it will automatic create link even it's http, https, ftp and ftps url.
sometime we simply save data without any a tag and when we display that content in our page then we need to display as link in web page.
Let's see bellow simple examples here:
Example
<?php
$myString = "Hi Rahul, This is a link: https://www.itsolutionstuff.com ";
$convertedString = convertURLs($myString);
print_r($convertedString);
function convertURLs($string)
{
$url = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/';
return preg_replace($url, '<a href="$0" target="_blank" title="$0">$0</a>', $string);
}
?>
Output:
Hi Rahul, This is a
link: <a href="https://www.itsolutionstuff.com" target="_blank" title="https://www.itsolutionstuff.com">https://www.itsolutionstuff.com</a>
You can see bellow layout output:
I hope it can help you...
Do you like below Tutorials ?
- Laravel 5.6 - Collection could not be converted to int
- Laravel - How to generate secure https URL from route?
- Laravel - Vue JS File Upload Example
- How to get last 7 days data in Laravel?
- Laravel Validation required if other field empty example
- Laravel Eloquent - When Case Statement in Select Query Example
- Laravel 7.x and 6.x Passing Variable to Javascript Example
- How to pass PHP variables in JavaScript or jQuery?