Home  >  Article  >  Backend Development  >  How to Mimic Stack Overflow\'s Auto-Link Behavior with PHP?

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

Susan Sarandon
Susan SarandonOriginal
2024-11-02 00:32:31824browse

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>

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>

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:

<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>

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!

Statement:
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