導航:首頁 > 凈水問答 > php過濾html保留img

php過濾html保留img

發布時間:2022-12-14 10:42:28

『壹』 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標簽, 只留下內容.

閱讀全文

與php過濾html保留img相關的資料

熱點內容
印染廢水中cod排放量是多少 瀏覽:245
冷干機的濾芯如何拆下來 瀏覽:552
海爾凈水器出水管介面怎麼拆 瀏覽:13
河北水垢漏斗 瀏覽:689
白雲區農村ppp污水項目 瀏覽:498
安吉爾水壺濾芯怎麼拆 瀏覽:318
電廠化學廢水調整及注意事項 瀏覽:892
什麼叫納米微晶技術凈化器 瀏覽:43
百佳境界凈水器如何 瀏覽:695
甲醇蒸餾塔再沸器的原理 瀏覽:268
ro膜氯化 瀏覽:984
潔廁靈能除垢 瀏覽:459
油煙機凈化器的價格多少錢一台 瀏覽:334
凈化器電源怎麼測量 瀏覽:332
wq污水提升泵 瀏覽:415
污水處理50戶需多少立方池 瀏覽:656
樹脂是不是ab膠 瀏覽:694
減壓蒸餾怎麼拆 瀏覽:544
飲水機為什麼加熱一會就保溫 瀏覽:287
電解法處理污水基於什麼原理 瀏覽:229