How to Mimic Stack Overflow\'s Auto-Link Behavior with PHP?

Susan Sarandon
Release: 2024-11-02 00:32:31
Original
823 people have browsed it

How to Mimic Stack Overflow's Auto-Link Behavior with PHP?

Mimicking Stack Overflow's Automated Link Generation with PHP

Stack Overflow's user-friendly auto-linking feature enhances user engagement and information accessibility. This article presents a PHP function inspired by this capability, seamlessly converting URLs into hyperlinked content.

The provided function utilizes a robust regex pattern to identify URLs. It then extracts essential components from the URL, such as the domain and path. By truncating overly long URLs, it ensures a visually appealing and concise display.

Function Implementation:

<code class="php">function auto_link_text($text)
{
    // Daring Fireball's URL-matching regex pattern
    $pattern = '(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))';

    // Regex callback function for URL processing
    $callback = create_function('$matches', '
        $url       = array_shift($matches);
        $url_parts = parse_url($url);

        $text = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
        $text = preg_replace("/^www./", "", $text);

        $last = -(strlen(strrchr($text, "/"))) + 1;
        if ($last < 0) {
            $text = substr($text, 0, $last) . "&amp;hellip;";
        }

        return sprintf(\'<a rel="nofollow" href="%s">%s</a>\', $url, $text);
    ');

    return preg_replace_callback($pattern, $callback, $text);
}</code>
Copy after login

Usage Example:

<code class="php">$input_text = "This is my text.  I wonder if you know about asking questions on StackOverflow:
 Check This out http://www.stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior

 Also, base_convert php function?
http://pt.php.net/manual/en/function.base-convert.php#52450

http://pt.php.net/manual/en/function.base-convert.php?wtf=hehe#52450";

$output_text = auto_link_text($input_text);</code>
Copy after login

Input Text:

This is my text.  I wonder if you know about asking questions on StackOverflow:
Check This out http://www.stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior

Also, base_convert php function?
http://pt.php.net/manual/en/function.base-convert.php#52450

http://pt.php.net/manual/en/function.base-convert.php?wtf=hehe#52450
Copy after login

Output Text:

<code class="html">This is my text.  I wonder if you know about asking questions on StackOverflow:
Check This out <a rel="nofollow" href="http://www.stackoverflow.com/questions/1925455/how-to-mimic-stackoverflow-auto-link-behavior">stackoverflow.com/questions/1925455/&hellip;</a>

Also, base_convert php function?
<a rel="nofollow" href="http://pt.php.net/manual/en/function.base-convert.php#52450">pt.php.net/manual/en/&hellip;</a>

<a rel="nofollow" href="http://pt.php.net/manual/en/function.base-convert.php?wtf=hehe#52450">pt.php.net/manual/en/&hellip;</a></code>
Copy after login

This custom PHP function effectively emulates the Stack Overflow auto-linking feature, transforming URLs into hyperlinks for enhanced readability and user experience.

The above is the detailed content of How to Mimic Stack Overflow\'s Auto-Link Behavior with PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!