Home  >  Article  >  Backend Development  >  PHP function example to delete string HTML tags

PHP function example to delete string HTML tags

WBOY
WBOYOriginal
2016-07-25 08:54:161030browse
  1. //删除字符串中html标签
  2. //by bbs.it-home.org
  3. function delete_htm($scr)
  4. {
  5. for($i=0;$i{
  6. if(substr($scr,$i,1)=="<")
  7. {
  8. while(substr($scr,$i,1)!=">")$i++;
  9. $i++;
  10. }
  11. $str=$str.substr($scr,$i,1);
  12. }
  13. return($str);
  14. }
  15. ?>
复制代码

PHP如何去除HTML标签

方法1: 直接取出想要取出的标记

  1. //取出br标记
  2. function strip($str)
  3. {
  4. $str=str_replace("<br>","",$str);
  5. //$str=htmlspecialchars($str);
  6. return strip_tags($str);
  7. }
  8. ?>
复制代码

方法2. PHP 中有个 strip_tags 函数可以方便地去除 HTML 标签。 echo strip_tags(“Hello World”); // 去除 HTML、XML 以及 PHP 的标签。 对于非标准的 HTML 代码也能正确的去除: echo strip_tags(“\”>cftea”); //输出 cftea

strip_tags函数去除HTML标签:

  1. $str = ‘www

    dreamdu

    .com’;
  2. echo(htmlspecialchars($str).”
    ”);
  3. echo(strip_tags($str));
  4. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn