<? // 模式定界符后面的 "i" 表示不区分大小写字母的搜索 if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) { print "A match was found."; } else { print "A match was not found."; } ?> |
<? /* 模式中的 b 表示单词的边界,因此只有独立的 "web" 单词会被匹配, * 而不会匹配例如 "webbing" 或 "cobweb" 中的一部分 */ if (preg_match ("/bwebb/i", "PHP is the web scripting language of choice.")) { print "A match was found."; } else { print "A match was not found."; } if (preg_match ("/bwebb/i", "PHP is the website scripting language of choice.")) { print "A match was found."; } else { print "A match was not found."; } ?> |
<?php // 从 URL 中取得主机名 preg_match("/^(http://)?([^/]+)/i", "http://www.php.net/index.html", $matches); $host = $matches[2]; // 从主机名中取得后面两段 preg_match("/[^./]+.[^./]+$/", $host, $matches); echo "domain name is: {$matches[0]}n"; ?> |