本文目錄一覽:
- 1、php 過濾body里的東西
- 2、php正則提取頁面body和body之間的內容
- 3、求PHP提取html中 body 內容 的正則表達式。會追加分數。
- 4、php curl 判斷body是否有內容
- 5、求問一個php輸出內容在body內怎樣解決
- 6、在寫php 文件時,在標籤想添加一部分從資料庫中度數據的操作,代碼如下:
php 過濾body里的東西
?php
//保留標籤
function strip_word_html($text, $allowed_tags = ‘bisupsubemstrongubr’)
{
mb_regex_encoding(‘UTF-8’);
$search = array(‘/lsquo;/u’, ‘/rsquo;/u’, ‘/ldquo;/u’, ‘/rdquo;/u’, ‘/mdash;/u’);
$replace = array(‘\”, ‘\”, ‘”‘, ‘”‘, ‘-‘);
$text = preg_replace($search, $replace, $text);
$text = html_entity_decode($text, ENT_QUOTES, ‘UTF-8’);
if(mb_stripos($text, ‘/*’) !== FALSE){
$text = mb_eregi_replace(‘#/\*.*?\*/#s’, ”, $text, ‘m’);
}
$text = preg_replace(array(‘/([0-9]+)/’), array(‘ $1’), $text);
$text = strip_tags($text, $allowed_tags);
//$text = preg_replace(array(‘/^\s\s+/’, ‘/\s\s+$/’, ‘/\s\s+/u’), array(”, ”, ”), $text);
$search = array(‘#(strong|b)[^]*(.*?)/(strong|b)#isu’, ‘#(em|i)[^]*(.*?)/(em|i)#isu’, ‘#u[^]*(.*?)/u#isu’);
$replace = array(‘b$2/b’, ‘i$2/i’, ‘u$1/u’);
$text = preg_replace($search, $replace, $text);
$num_matches = preg_match_all(“/\!–/u”, $text, $matches);
if($num_matches){
$text = preg_replace(‘/\!–(.)*–\/isu’, ”, $text);
}
return $text;
}
//去掉屬性
function clean_inside_tags($txt,$tags){
preg_match_all(“/([^]+)/i”,$tags,$allTags,PREG_PATTERN_ORDER);
foreach ($allTags[1] as $tag){
$txt = preg_replace(“/”.$tag.”[^]*/i”,””.$tag.””,$txt);
}
return $txt;
}
//用來過濾html
function trmhtml($text){
$rm = ‘pimgbr’;
$text = str_replace(“nbsp;”,””,$text);
$text = strip_word_html($text, $rm);
$cit = clean_inside_tags($text, ‘pimg’);
$cit = str_replace(“nbsp;”,””,$cit);
return $cit;
}
//使用
$str = ‘div class=”nav_ent”psss/p
div class=”nav_ent1_1″a href=”/”img src=”/news/images/topcopy_20130809.jpg” //a/div
div class=”nav_ent1″a href=”/news/”img src=”/news/images/news_t.jpg” alt=”新聞” border=”0″/a/div
div class=”nav_ent2″table width=”100%” class=43160 cellpadding=”0″ cellspacing=”0″trtd valign=topA href=”/world/” target=_top國際/Anbsp;nbsp;A href=”/china/” target=_top時政/Anbsp;nbsp;A href=”/opinion/” target=_top評論/Anbsp;nbsp;A href=”/photo/” target=_blank圖片/Anbsp; A href=”/world/paper.htm” target=_blank外媒/Anbsp; A href=”/news/live/” target=_top直播/Anbsp; A href=”/news/special.htm” target=_top專題/Anbsp; A href=”/news/update.htm” target=_blank滾動/A/td/trtrtd align=right colspan=1/td/tr/table/div
/div
div class=”clearit”id=”top” name=”top”/div
div class=”gotop”a href=”#top”nbsp;/a/div
/div’;
echo trmhtml($str);
?
試試吧,看是不是要這樣的效果!
php正則提取頁面body和body之間的內容
/body.*?(.*?)\/body/is
.*?最小匹配,如果去掉?號,則默認是貪婪匹配
而前面加了?:則表示.*?所匹配的結果不會保存在緩衝區內
求PHP提取html中 body 內容 的正則表達式。會追加分數。
“/(body)(.*?)(\/body)/”這個其實就比較對了,只是少了點模式修正符號
改成
“/(body)(.*?)(\/body)/is”
就可以了。
模式修正符號是很重要的。
下邊是書上的原話。
s:如果設置了此修正符,模式中的圓點字元「.」匹配所有字元,包括換行符。即將字元串視為單行,換行符看作普通字元看待。
php curl 判斷body是否有內容
$url = “這是一個鏈接”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
// 解析 HTML 的 body 區段
preg_match(“/body.*(.*)\/body/smUi”,$response, $htmlHeaders);
if(!count($htmlHeaders)){
echo “無法解析 body 區段”;
exit;
}elseif( count($htmlHeaders[1]) ){
echo “body的內容為” . $htmlHeaders[1];
exit;
}else{
echo “body沒有內容。”;
exit;
}
求問一個php輸出內容在body內怎樣解決
這問題非常好解決啊
比如如下
?php
//這裡是你的update等操作,假設操作成功
if ( 成功 ) {
echo ‘htmlhead這裡寫你的js或者css等/headbody成功!/body/html’;
die;
}
?
html
head
head
body
/body
/html
按照這種格式不就完了?
原理就是你要輸出提示的時候,重新輸出一次html文檔格式,輸出完成後,終止程序運行die()
這樣,原來有的html文檔格式不再輸出,而重新輸出一個html文檔,這也不就行了嘛!
當然,每次操作都來寫代碼輸出一個html文檔,顯然很麻煩累贅,也不易維護
那你可以封裝一個函數功能啊
比如
function alert($txt = ” ) {
echo ‘這裡寫html文檔格式即可’ . $txt;
die();
}
當操作提示的時候
if ( 成功 ) {
alert(‘成功’);
}
或者
if ( 失敗 ) {
alert(‘失敗’);
}
這樣不挺好的?
在寫php 文件時,在標籤想添加一部分從資料庫中度數據的操作,代碼如下:
輸出查詢結果用echo ,怎麼是=號呢。兄弟,我做php2年第一次見。你是不是吧寫php當jsp的方式寫了。
lispan?php echo date(‘m-d’,strtotime($row[‘add_time’]))?/spana href=”#”?php echo $row[‘AlgID’]?/a/li
原創文章,作者:YXYR,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/149190.html