Heim >Backend-Entwicklung >PHP-Tutorial >PHP函数preg_match_all()的相关使用方法介绍_PHP教程
在下面是一个将文本中的URL链接地址转换为HTML代码的示例。代码6.4是PHP函数preg_match_all()的使用范例。
代码6.4 将文本中的链接地址转成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>
希望大家通过以上PHP函数preg_match_all()的使用示例,能够充分掌握这一函数的应用。