Skip to content
PHP Logo

Is it possible to automatically add [url] tags to URLs in PHP without using JavaScript?

When displaying URLs in HTML using PHP, it is important to properly format them with the [url] tags to make them clickable. This can be achieved by using the PHP function `preg_replace` along with a regular expression to detect URLs and add the necessary tags. By using this method, you can automatically add [url] tags to URLs without relying on JavaScript.

php
<?php
function addUrlTags($text) {
    return preg_replace('/(https?:\/\/[^\s]+)/', '<a href="$1" target="_blank">$1</a>', $text);
}

$url = "Check out this website: https://www.example.com";
echo addUrlTags($url);
?>