簡體   English   中英

"從 php 字符串中刪除所有 html 標簽"

[英]Remove all html tags from php string

我想顯示數據庫條目的前 110 個字符。 到目前為止很容易:

<?php echo substr($row_get_Business['business_description'],0,110) . "..."; ?>

使用strip_tags<\/code>

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);   //output Test paragraph. Other text

<?php echo substr(strip_tags($row_get_Business['business_description']),0,110) . "..."; ?>

使用 PHP 的strip_tags() 函數<\/a>。

例如:<\/strong>

$businessDesc = strip_tags($row_get_Business['business_description']);
$businessDesc = substr($businessDesc, 0, 110);


print($businessDesc);

從帶有內容的 PHP 字符串中刪除所有 HTML 標記!<\/h1>

假設您的字符串包含錨標記,並且您想刪除帶有內容的此標記,那么此方法將很有幫助。

輸出:

Lorem Ipsum 只是印刷和排版行業的虛擬文本。

使用這個正則表達式: \/<[^<]+?>\/g<\/code>

$val = preg_replace('/<[^<]+?>/g', ' ', $row_get_Business['business_description']);

$businessDesc = substr(val,0,110);

對我來說這是最好的解決方案。

function strip_tags_content($string) { 
    // ----- remove HTML TAGs ----- 
    $string = preg_replace ('/<[^>]*>/', ' ', $string); 
    // ----- remove control characters ----- 
    $string = str_replace("\r", '', $string);
    $string = str_replace("\n", ' ', $string);
    $string = str_replace("\t", ' ', $string);
    // ----- remove multiple spaces ----- 
    $string = trim(preg_replace('/ {2,}/', ' ', $string));
    return $string; 

}

以防萬一, fgetss()在刪除/剝離所有html和php標簽的行中讀取文件。

在 laravel 中,您可以使用以下語法

 @php
   $description='<p>Rolling coverage</p><ul><li><a href="http://xys.com">Brexit deal: May admits she would have </a><br></li></ul></p>'
 @endphp
 {{  strip_tags($description)}}

<?php $data = "<div><p>Welcome to my PHP class, we are glad you are here

"; echo strip_tags($data); ?><\/code>

或者,如果您有來自數據庫的內容;

<?php $data = strip_tags($get_row['description']); ?><\/code> <?=substr($data, 0, 100) ?><?php if(strlen($data) > 100) { ?>...<?php } ?><\/code>

$string = <p>Awesome</p><b> Website</b><i> by Narayan</i>. Thanks for visiting enter code here;
$tags = array("p", "i");

echo preg_replace('#<(' . implode( '|', $tags) . ')(?:[^>]+)?>.*?</\1>#s', '', $string);

從 HTML 標簽中去除字符串:

<?php
echo strip_tags("Hello <b>world!</b>");
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM