Home > Article > Backend Development > Introduction to the related usage of PHP function preg_match_all()_PHP tutorial
Below is an example of converting a URL link address in text into HTML code. Listing 6.4 is an example of using the PHP function preg_match_all().
Code 6.4 Convert the link address in the text into HTML
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li><span>//功能:将文本中的链接地址转成HTML </span></li> <li class="alt"><span>//输入:字符串 </span></li> <li><span>//输出:字符串 </span></li> <li class="alt"><span>function url2html($text) </span></li> <li><span>{ </span></li> <li class="alt"><span>//匹配一个URL,直到出现空白为止 </span></li> <li><span>preg_match_all("/http://?[^s]+/i"<br>, $text, $links); </span></li> <li class="alt"><span>//设置页面显示URL地址的长度 </span></li> <li> <span>$</span><span class="attribute">max_size</span><span> = </span><span class="attribute-value">40</span><span>; </span> </li> <li class="alt"><span>foreach($links[0] as $link_url) </span></li> <li><span>{ </span></li> <li class="alt"><span>//计算URL的长度。如果超过$max_size的设置,则缩短。 </span></li> <li> <span>$</span><span class="attribute">len</span><span> = </span><span class="attribute-value">strlen</span><span>($link_url); </span> </li> <li class="alt"> <span>if($len </span><span class="tag">></span><span> $max_size) </span> </li> <li><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute">link_text</span><span> = </span><span class="attribute-value">substr</span><span>($link_url, 0, $max_size)."..."; </span> </li> <li><span>} else { </span></li> <li class="alt"> <span>$</span><span class="attribute">link_text</span><span> = $link_url; </span> </li> <li><span>} </span></li> <li class="alt"><span>//生成HTML文字 </span></li> <li> <span>$</span><span class="attribute">text</span><span> = </span><span class="attribute-value">str_replace</span><span>($link_url,"<br></span><span class="tag"><span> </span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">'$link_url'</span><span class="tag">></span><span>$link_text</span><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span>",$text); </span></span> </li> <li class="alt"><span>} </span></li> <li><span>return $text; </span></li> <li class="alt"><span>} </span></li> <li><span>//运行实例 </span></li> <li class="alt"> <span>$</span><span class="attribute">str</span><span> = “这是一个包含多个URL链接地址的多行文字。<br>欢迎访问http://www.taoboor.com”; </span> </li> <li><span>print url2html($str); </span></li> <li class="alt"><span>/*输出结果 </span></li> <li> <span>这是一个包含多个URL链接地址的多行文字<br>。欢迎访问</span><span class="tag"><span> </span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">'http://www.taoboor.com'</span><span class="tag">></span><span> </span></span> </li> <li class="alt"> <span>http://www.taoboor.com</span><span class="tag"></span><span class="tag-name">a</span><span class="tag">></span><span> </span> </li> <li><span>*/ </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
I hope you can fully grasp the application of this function through the above usage examples of the PHP function preg_match_all().