PHP Find URL in Text (String) and Make Link Example
Today our leading topic is php find url in text and make link. This post will give you simple example of php find url in string and make link. if you want to see example of find url in string and mke link in php then you are a right place. this example will help you how to find url in text and display as 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- JQuery Open Link in New Window Example
- How to Split String to Array By Comma using JQuery?
- How to Make Read More Link from String in PHP?
- How to Remove White Space from String in PHP?
- PHP Check Word Exist in String or Not Example
- How to Check If String is URL or Not in PHP?
- Mediafire PHP File Uploading and Get Link Example
- How to Add Query String Automatically on Laravel Pagination Links?
- How to Generate Random Alphanumeric String in PHP?