正則表達式在程序開發中是非常有用的,用好正則我們可以搜索、驗證及替換文本或任何類型的字符。在這篇文章中,UncleToo為大家搜集了15個開發過程中常用的PHP正則表達式、函數及PHP示例,學習這些你會發現正則有多么強大。
從字符串中刪除特定字符:本段代碼實現刪除字符串中除所有大小寫字母及數字以外的字符。
|
1
2
3
4
5
|
<?php
$value
=
"wWw.nongyejing.Com - 【nongyejing】 - 12345"
;
$value
= preg_replace(
"/[^A-Za-z0-9]/"
,
""
,
$value
);
echo
$value
;
?>
|
驗證用戶名:以下代碼驗證用戶名是否由字母、數字及下划線組成。
<?php
$username = "nongyejing_COM123"; if (preg_match('/^[a-z\d_]{5,20}$/i', $username)) { echo "用戶名可用"; } else { echo "用戶名存在特殊字符"; } ?>
添加信息到圖片alt屬性:使用下面函數,可以實現將文章標題添加到圖片的alt屬性中。
<?php
function add_alt_tags($content) { global $post; preg_match_all('/<img (.*?)\/ >/', $content, $images); if(!is_null($images)) { foreach($images[1] as $index => $value) { if(!preg_match('/alt=/', $value)) { $new_img = str_replace('<img', '<img alt="'.get_the_title().'"', $images[0][$index]); $content = str_replace($images[0][$index], $new_img, $content); } } } return $content; } ?>
將EMail文本自動添加Mailto鏈接
<?php
$text = "demo@abc.com"; $string = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a href="mailto:\\1">\\1</ a>', $text); echo $string; ?>
過濾限制級詞語
<?php
function filtrado($texto, $reemplazo = false) { $filtradas = 'admin,nongyejing,農業經'; //這里定義需要過濾的詞語 $f = explode(',', $filtradas); $f = array_map('trim', $f); $filtro = implode('|', $f); return ($reemplazo) ? preg_replace("#$filtro#i", $reemplazo, $texto) : preg_match("#$filtro#i", $texto) ; } ?>
驗證電話號碼:這是一個很常見的功能 例如:展報
<?php
$string = "(010) 555-5555"; if (preg_match('/^\(?[0-9]{3}\)?|[0-9]{3}[-. ]? [0-9]{3}[-. ]?[0-9]{4}$/', $string)) { echo "successful."; } ?>
替換超鏈接href屬性的內容:
<?php
$html = '<a href="http://www.nongyejing.com">農業經</a>'; $replacement = "yes"; $pattern = '/(?<=href\=")[^]]+?(?=")/'; $replacedHrefHtml = preg_replace($pattern, $replacement, $html); echo $replacedHrefHtml ; ?>
在頁面上查看源文件,顯示為:<a href="yes">農業經</a>
驗證郵箱正則表達式:此功能在用戶注冊是經常使用
<?php
$regex = "([a-z0-9_.-]+)". # name "@". # at "([a-z0-9.-]+){2,255}". # domain & possibly subdomains ".". # period "([a-z]+){2,10}"; # domain extension $eregi = eregi_replace($regex, '', $email); $valid_email = empty($eregi) ? true : false; ?>
IP地址驗證
<?php
$string = "255.255.255.255"; if (preg_match( '/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $string)) { echo "IP address is good."; } ?>
郵政編碼驗證
<?php
$string = "12345-1234"; if (preg_match('/^[0-9]{5}([- ]?[0-9]{4})?$/', $string)) { echo "zip code checks out"; } ?>
高亮顯示文本:
<?php
<?php $text = "農業經(www.nongyejing.com)農業經網; $text = preg_replace("/\b(www)\b/i", '<span style="background:#5fc9f6">\1</ span>', $text); echo $text; ?>
從特定的URL中提取域名;輸出:www.jiaxiangz.com
<?php
$url = "http://www.jiaxiangz.com/plug/tags/?tag=嘉祥新聞"; preg_match('@^(?:http://)?([^/]+)@i', $url, $matches); $host = $matches[1]; echo $host; ?>
驗證域名格式是否正確:
<?php
$url = "http://www.453s.com/"; if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) { echo "域名格式正確."; } else { echo "域名格式錯誤."; } ?>
使用文章標題生成URL:輸出:my-name-is-nongyejing
<?php
function create_slug($string){ $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string); return $slug; } echo create_slug('my name is nongyejing'); ?>
添加http://到URL地址
當我們需要用戶填寫網址時,很多用戶往往不填寫http://直接輸入域名,使用下面代碼可將http://添加到網址的前面。
<%
if (!preg_match("/^(http|https|ftp):/", $_POST['url'])) { $_POST['url'] = 'http://'.$_POST['url']; } %>
將URL轉換為超鏈接
這時一個很有用的功能,他可以將url地址或email地址轉換為可點擊的超鏈接文本。例如:國畫網
<?php
function makeLinks($text) { $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)','\1', $text); $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)','\1\2', $text); $text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})','\1', $text); return $text; } ?>

