PHP对文本内的URL进行Auto Link的处理

文本中会出现有 http:// 这样的链接,我们要使用PHP对它进行处理,显示在页面上的时候自动加上一个链接。

解决方案:使用PHP正则式。

代码如下:

function url_auto_link($input_string, $max_url_length) {
 preg_match_all("/(^|[^="'])(https?|ftp|news)(://[[:alnum:]+$;?.%,!#~*/:@&=_-]+)/",$input_string,$out,PREG_SET_ORDER);
 for ($i=0;$i $max_url_length){
        $url_short_string = substr($url_string,0,$max_url_length) . '...';
    } else {
        $url_short_string = $url_string;
    }
    $input_string = str_replace($url_string,"<a href="$url_string" target="_blank">$url_short_string</a>",$input_string);
 }
 print $input_string;
}

( ! ) Warning: Missing argument 1 for cwppos_show_review(), called in /data/www/enjoyphp/wp-content/themes/flat/content-single.php on line 29 and defined in /data/www/enjoyphp/wp-content/plugins/wp-product-review/includes/legacy.php on line 18
Call Stack
#TimeMemoryFunctionLocation
10.0001355192{main}( ).../index.php:0
20.0001355480require( '/data/www/enjoyphp/wp-blog-header.php' ).../index.php:17
30.182710153728require_once( '/data/www/enjoyphp/wp-includes/template-loader.php' ).../wp-blog-header.php:19
40.184610224872include( '/data/www/enjoyphp/wp-content/themes/flat/single.php' ).../template-loader.php:74
50.275310782184get_template_part( ).../single.php:5
60.275310782704locate_template( ).../general-template.php:167
70.275310782800load_template( ).../template.php:643
80.275310783184require( '/data/www/enjoyphp/wp-content/themes/flat/content-single.php' ).../template.php:686
90.280110768296cwppos_show_review( ).../content-single.php:29

发表评论

电子邮件地址不会被公开。 必填项已用*标注