『壹』 php 過濾掉html標簽及標簽內的所有內容
方法一:使用strip_tags()函數
strip_tags() 函數剝去字元串中的 HTML、XML 以及PHP的標簽。
使用內案例:
$string = "<p>這里是容潘旭博客</p>"
$newStr = strip_tags($string);
echo $newStr;
方法二:使用str_replace()函數
str_replace() 函數以其他字元替換字元串中的一些字元(區分大小寫)
使用案例:
$string = "<p>這里是潘旭博客</p>";
$newStr = str_replace(array("<p>","</p>"),array("",""));
echo $newStr;
另外還有一種是通過正則的方法,請參考:https://panxu.net/article/8385.html
『貳』 php 文章需要過濾掉img標簽
PHP的preg_replace函數是 執行一個正則表達式的搜索和替換
語法
1:preg_replace (pattern ,replacement ,subject,limit,count )
參數
描述
pattern 正則表達式(字元串或字元串數組)
replacement 用於替換的字元串或字元串數組
subject 要進行搜索和替換的字元串或字元串數組。
limit 可選。每個模式在每個subject上進行替換的最大次數。默認是 -1(無限)。
cout 可選。完成的替換次數
示例:
<?php//把heigth高度屬性刪除,並添加width="100%"
$str='<div><p>12312321</p><imgsrc="xx.jpg"height="213"/><span>111</span><imgsrc="xz.jpg"/></div>';
$str=preg_replace("/height="[0-9]+?"/","",$str);
$str1=preg_replace("/src="(.+?)"/","src="$1"width="100%"",$str);
print_r($str1);
?>
『叄』 php 處理 html 圖片標簽
過濾img標簽:
單獨提取img標簽:
代碼如下:
<?php
$text='<strong>這是一個測試顯示的簡單例子</strong>
<imgborder="0"alt=""src="/attachments/users/2013/09/17/20130917144341_45299.jpg"width="300"height="180"/><spanstyle="background-color:#e53333;font-size:14px;">求解答</span>';
//$text是你獲取的文本
//過濾img標簽
$no_img=preg_replace(''<img[^>]*?>'','',$text);
//單獨提取img標簽
preg_match_all(''<img[^>]*?>'',$text,&$img);
//$no_img就是過濾後的文本
//$img是一個數組,每一個值都是一個img標簽
?>
這樣就可以了。
『肆』 正則表達式去 去html標簽留img
如果你是想過濾掉所有除去<img>外的其他標簽。
java源代碼如下:
String test = "234<strong>324</strong>324<em>32<a href=\"#\">4te</a>st1</em>2<img src=\"test.jpg\" />3";
System.out.println(test);
System.out.println(test.replaceAll("<(?!img)[^>]*>",""));
輸出的結果是:
234<strong>324</strong>324<em>32<a href="#">4te</a>st1</em>2<img src="test.jpg" />3
234324324324test12<img src="test.jpg" />3
其他的語言也類似,用支持正則的替換函數就可以了,1步實現。
比如php用
$strnew = preg_replace("#<(?!img)[^>]*>#","",$str);
『伍』 PHP 過濾HTML中除了img標簽外其它所有標簽,同時保留標簽內容,但<script>標簽內的內容都清除。
提供實例:
<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo "\n";
// 允許 <p> 和 <a>
echo strip_tags($text, '<p><a>');
?>
以上常式會輸出:版
Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
具體做權法:
<?php
echo strip_tags($text, 'img');
?>
『陸』 php正則表達式過濾某些HTML標簽代碼
如果只要
<b>
標簽,不用「過濾」的方法,用「提取」的方法更簡單。
$str
=
'<img
src="xxx"><b>aaa</b><br>\n<b>b\nbb</b><span
style="color:#FF0000;">yyy</span>';
$pattern
=
'/<b>(((?!<\/b>).)*)<\/b>/mi';
preg_match_all($pattern,
$str,
$matches,
PREG_SET_ORDER);
print_r($matches);
輸出
Array
(
[0]
=>
Array
(
[0]
=>
<b>aaa</b>
[1]
=>
aaa
[2]
=>
a
)
[1]
=>
Array
(
[0]
=>
<b>b\nbb</b>
[1]
=>
b\nbb
[2]
=>
b
)
)
$matches[0][0],$matches[1][0]
是你想要的結果?
『柒』 php 過濾HTML格式但得保留圖片的正則表達式
花了點時間,試了N種終於搞定了..有點難度!
<?php
$str = '<div><span><table><tr><td>我ftyrtyrt<b>是</b>中<strong>國</strong>人<img src=images/logo.jpg></td></tr></table></span></div>';
preg_match('/(>[^<](.+)?\.jpg>)/', $str, $arr);
// print_r($arr); // 為什麼這會是亂碼?不解?
echo substr($arr[0], 1);
?>
『捌』 誰給個php過濾img標簽 只留下地址的表達式
preg_match_all('@<img .*src="(.*)" .* />@Ui',$string,$img);
$img=$img[1];
『玖』 php文件輸出如何過濾掉html,代碼如下
<b>asasasas</b>這個html標簽是加粗標簽,如果你想在瀏覽器上顯示的是版加粗的asasasas就直接輸出
<?php
echo "<b>asasasas</b>";
?>
如果你想輸權出的<b>asasasas</b>這個字元串的話呢
<?php
echo htmlspecialchars("<b>asasasas</b>");
?>
『拾』 php怎麼過濾掉html啊,比如「[color=#111111]都說」
你可以看下這個函數是不是符合你的要求: strip_tags()
這個函數的作用是去除字元串中的html標簽, 只留下內容.