A. php正則表達來獲取html中的部分內容
1、幾種函數的簡單說明:
(1)preg_grep -- 返回與模式匹配的數組單元
preg_grep 返回一個input 數組中與給定的 pattern 模式相匹配的單元所組成的數組。
(2)preg_match -- 進行正則表達式匹配
說明:int preg_match ( string pattern, string subject [, array matches [, int flags]] )在 subject 字元串中搜索與 pattern 給出的正則表達式相匹配的內容。 返回值0或1。
(3)preg_replace -- 執行正則表達式的搜索和替換
(4)preg_split -- 用正則表達式分割字元串
2. 一般來說,如果用正則來獲取匹配內容,一般使用 preg_match/ preg_match_all 函數。
補充回答:
正則表達式如下:
'%<div class="wap2"><span>功能</span>(.*?)</div>%si'
以下是代碼,經測試,運行正常:
<?php
$str = '<div class="wap2"><span>功能</span>這裡面是要獲取的內容,不能確定什麼字元,可能是數字(123456…)字母(badn…),特殊浮等(o_O\(^o^)/…)</div>';
$pa = '%<div class="wap2"><span>功能</span>(.*?)</div>%si';
preg_match($pa,$str,$r);
echo $r[1];
?>
B. PHP的preg_replace 正則替換
preg_replace("/http:\/\//","",$message)
$message = preg_replace(array(
"/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies",
"/\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies",
"/http:\/\//" //加的,,
), $allowimgcode ? array(
"bbcodeurl('\\1', '<img src=\"%s\" onload=\"thumbImg(this)\" alt=\"\" />')",
"bbcodeurl('\\3', '<img width=\"\\1\" height=\"\\2\" src=\"%s\" border=\"0\" alt=\"\" />')"
) : array(
"bbcodeurl('\\1', '<a href=\"%s\" target=\"_blank\">%s</a>')",
"bbcodeurl('\\3', '<a href=\"%s\" target=\"_blank\">%s</a>')"
), $message);
其實你也可以在它處理完後加preg_replace("/http:\/\//","",$message)這句.
C. php 去掉 「<--」 到 「-->」 之間的字元 用正則表達式
$str = preg_replace('/<--(.+?)-->/', '', $str);
D. 用php中的preg_match_all匹配網頁上div的一段代碼,正則表達式該怎麼寫
<?php
$str='<div id="aaa111" name="bbb-bb" class="ccccc ddd">這里是我需要的代碼</div>';
$str=preg_replace("/<div id=\"aaa(.*)\">(.*)<\\/div>/iU","$2",$str); //你要想的代碼
echo $str;
?>
E. php中的 preg_replace() 怎麼用 preg_replace_callback()表示
preg_replace 是正則替換,正則就是一個用通配符的表達式。要想知道怎麼用,首先要去學習下正則,如果你的基礎還可以,找個視頻聽一聽,半個小時就可以入門了。