php 如何检查字符串内容中是否包含任何 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10778035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:02:13  来源:igfitidea点击:

How to check if string contents have any HTML in it?

phphtmlregexxhtml

提问by nico

How can I check if PHP string contents contain any HTML contents?

如何检查 PHP 字符串内容是否包含任何 HTML 内容?

I'm not good with Regular Expressions so I would like to have a function named "is_html" to check this. :) thank you!

我不擅长正则表达式,所以我想要一个名为“ is_html”的函数来检查这个。:) 谢谢你!

回答by nico

If you want to test if a string contains a "<something>", (which is lazy but can work for you), you can try something like that :

如果你想测试一个字符串是否包含"<something>", (这很懒但可以为你工作),你可以尝试这样的事情:

function is_html($string)
{
  return preg_match("/<[^<]+>/",$string,$m) != 0;
}

回答by ???? ?????

Instead of using regex (like the other suggestions here) I use the following method:

我没有使用正则表达式(就像这里的其他建议一样),而是使用以下方法:

    function isHtml($string)
    {
        if ( $string != strip_tags($string) )
        {
            return true; // Contains HTML
        }
        return false; // Does not contain HTML
    }

Here I use a PHP function strip_tagsto remove any HTML from the string. It then compares the strings and if they do not match HTML tags were present.

在这里,我使用 PHP 函数strip_tags从字符串中删除任何 HTML。然后比较字符串,如果它们不匹配,则存在 HTML 标记。

回答by Kevin Traas

The accepted answer will consider a string containing <something> as HTML which, obviously, it is not.

接受的答案会将包含 <something> 的字符串视为 HTML,显然,它不是。

I use the following, which may or may not be a better idea. (Comments appreciated.)

我使用以下内容,这可能是也可能不是更好的主意。(评论赞赏。)

function isHTML( $str ) { return preg_match( "/\/[a-z]*>/i", $str ) != 0; }

This looks for any string containing /> with zero or more letters between the slash and closing bracket.

这会查找任何包含 /> 的字符串,在斜杠和右括号之间有零个或多个字母。

The above function returns:

上面的函数返回:

<something>             is NOT HTML
<b>foo</b>              is HTML
<B>foo</B>              is HTML
<b>foo<b>               is NOT HTML
<input />               is HTML

回答by Ian Wood

probably the easiest way would be something like:

可能最简单的方法是:

<?php

function hasTags( $str )
{
    return !(strcmp( $str, strip_tags($str ) ) == 0);
}

$str1 = '<p>something with <a href="/some/url">html</a> in.';
$str2 = 'a string.';

var_dump( hasTags( $str1 ) ); // true - has tags.
var_dump( hasTags( $str2 ) ); // false - no tags.

回答by Constantine Loukas

Here's what I came up with

这是我想出的

function isHtml($string){
     preg_match("/<\/?\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>/",$string, $matches);
     if(count($matches)==0){
        return FALSE;
      }else{
         return TRUE;
      }
}

You just pass a string and check if it returns true or false. As simple as that.

您只需传递一个字符串并检查它是否返回 true 或 false。就如此容易。

回答by buckley

That depends on what you define to be html contents.

这取决于您定义的 html 内容。

The most straightforward thing is to test if the string contains the html tag which can be done with the regex

最直接的事情是测试字符串是否包含可以使用正则表达式完成的 html 标记

<html.*>

In php the test will be

在 php 中,测试将是

if (preg_match('/<html.*>/', $subject)) {
    # Successful match
} else {
    # Match attempt failed
}

If you want to see you have valid html it's better to use a html parser.

如果您想查看您拥有有效的 html,最好使用 html 解析器。