Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Encode flat text URL to clickable URL

I have developed a nice little FAQ for my site. Everything is stored in a MySQL database from Category, Question and Answer, the collumns is VARCHAR (255). I use the following code before adding anything I input to the database and only I have access to what gets added, edited or deleted.

htmlspecialchars($_POST['xxxxxx'])

What is the best way to get the data from the database and echo it out as clickable URL’s using PHP? Most of the information I have found seems to be 10 – 12 years old and many functions are now deprecated. I am not so sure security would not be an issue as it does not allow user input unless it is from myself. I know this question has been asked many times but I only find outdated code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This solution will catch all http/https/www and convert to clickable links.

Convert plain text URLs into HTML hyperlinks in PHP

$url = ‘/(http|https|ftp|ftps)://[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(/\S*)?/’;
$string= preg_replace($url, ‘$0’, $string);
echo $string;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading