Home >Backend Development >PHP Tutorial >How to Mimic Stack Overflow\'s Auto-Link Functionality in PHP?

How to Mimic Stack Overflow\'s Auto-Link Functionality in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-10-30 13:32:27712browse

How to Mimic Stack Overflow's Auto-Link Functionality in PHP?

Mimicking Stack Overflow's Auto-Link Functionality

To enhance web content, consider simulating Stack Overflow's automatic linking feature. This function enables the conversion of plain URLs into visually appealing hyperlinks.

Implementation in PHP

Here's a PHP function inspired by Stack Overflow's behavior:

<code class="php">function auto_link_text($text)
{
   // URL matching regex pattern from Daring Fireball
   $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`!()\[\]{};:\'".,<>?«»“”‘’]))';

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

Example Usage

Consider the following input text:

<code class="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</code>

Using the auto_link_text function, this text is transformed:

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

Customization

You can tailor the linking behavior by modifying the callback function. For instance, you could:

  • Alter the link text format
  • Add additional attributes to the link tag
  • Exclude certain URLs from auto-linking

By tweaking the implementation, you can create a custom auto-linking solution that meets your specific needs.

The above is the detailed content of How to Mimic Stack Overflow\'s Auto-Link Functionality in 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