Hello. I allow members to post statuses on my website and i want to check links. And if that text includes "www" i want to make that a link. How would i check this everytime i pull the status from the database to show it on the page

Dani AI

Generated

pointed the OP in the right direction and confirmed it worked — here’s an expanded, slightly safer approach you can run when printing statuses (not when saving). It detects http(s):// and www. links, prepends a scheme for bare www, validates the URL, escapes output to prevent XSS, and preserves trailing punctuation.

function linkify($text) {
    return preg_replace_callback(
        '/\b((?:https?:\/\/|www\.)[^\s<]+)/i',
        function($m) {
            $part = $m[1];
            // pull off common trailing punctuation
            $trail = '';
            while (strlen($part) && preg_match('/[.,:;!?)]+$/', $part)) {
                $trail = substr($part, -1) . $trail;
                $part = substr($part, 0, -1);
            }
            $href = preg_match('#^https?://#i', $part) ? $part : 'http://' . $part;
            if (!filter_var($href, FILTER_VALIDATE_URL)) {
                return htmlspecialchars($m[1], ENT_QUOTES, 'UTF-8') . $trail;
            }
            $safeHref = htmlspecialchars($href, ENT_QUOTES, 'UTF-8');
            $safeText = htmlspecialchars($part, ENT_QUOTES, 'UTF-8');
            return '<a href="'.$safeHref.'" target="_blank" rel="noopener noreferrer ugc">'.$safeText.'</a>'.$trail;
        },
        $text
    );
}

Notes and tips:

  • Validate with filter_var so malformed strings don't become clickable junk.
  • Escape both link text and href with htmlspecialchars to avoid XSS.
  • rel="noopener noreferrer ugc" protects users and signals user-generated links; use nofollow too if your moderation/policy requires it.
  • Keep raw user input in the DB and run this on render — that makes future changes to linking or sanitization easier.
  • Test edge cases: parentheses, punctuation, newlines, and domain-only strings like example.com (you can extend the regex if you need to match those, but do so carefully to avoid false positives).
  • If you allow any HTML from users, use a vetted sanitizer (e.g., HTML purifier libraries) rather than naive strip_tags.

This approach gives clickable, safe links while keeping rendering logic separate from storage and preserving security.

Recommended Answers

All 2 Replies

Here is a page with similar code http://css-tricks.com/snippets/php/find-urls-in-text-make-links/. As stated in the page all links must start with "http://", "https://" or the like for the regular expression to match it.

Sanitize the data before saving the status to the database but do not transform the text URLs into links until your script pulls the status from the database and run this function when printing out the status text to the browser.

Alls you should have to do is change the $text variable to whatever variable you declare for the status text.

If this seems to solve your problem then feel free to close the thread so others know that it is solved. If you have any other questions keep it open until you are satisfied.

Thank you this worked great! :)

Here is a page with similar code http://css-tricks.com/snippets/php/find-urls-in-text-make-links/. As stated in the page all links must start with "http://", "https://" or the like for the regular expression to match it.

Sanitize the data before saving the status to the database but do not transform the text URLs into links until your script pulls the status from the database and run this function when printing out the status text to the browser.

Alls you should have to do is change the $text variable to whatever variable you declare for the status text.

If this seems to solve your problem then feel free to close the thread so others know that it is solved. If you have any other questions keep it open until you are satisfied.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.