龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

10个实用的PHP正则表达式汇总(2)

时间:2014-10-24 11:21来源:网络整理 作者:网络 点击:
分享到:
复制代码 代码如下: $url = "http://ansoncheung.tk/"; if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(:\.[A-Z0-9][A-Z0-9_-]*)+):(\d+)\//i', $url)) { echo "Your url is ok."; } else {

复制代码 代码如下:
$url = "http://ansoncheung.tk/";
 if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) {
 echo "Your url is ok.";
 } else {
 echo "Wrong url.";
 }

9. 从特定URL中提取域名

复制代码 代码如下:
$url = "http://ansoncheung.tk/articles";
 preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
 $host = $matches[1];
echo $host;

10. 将文中关键词高亮显示

复制代码 代码如下:
$text = "Sample sentence from AnsonCheung.tk, regular expression has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex or regexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor";
$text = preg_replace("/\b(regex)\b/i", '<span style="background:#5fc9f6">\1</span>', $text);
echo $text;

希望本文所述对大家的PHP程序设计有所帮助。

精彩图集

赞助商链接