Detect HTML tags in a string

前端 未结 8 1489
渐次进展
渐次进展 2020-12-04 12:59

I need to detect whether a string contains HTML tags.

if(!preg_match(\'(?<=<)\\w+(?=[^<]*?>)\', $string)){ 
    return $string;
}
8条回答
  •  没有蜡笔的小新
    2020-12-04 13:45

    If purpose is just to check if string contain html tag or not. No matter html tags are valid or not. Then you can try this.

    function is_html($string) {
      // Check if string contains any html tags.
      return preg_match('/<\s?[^\>]*\/?\s?>/i', $string);
    }
    

    This works for all valid or invalid html tags. You can check confirm here https://regex101.com/r/2g7Fx4/3

提交回复
热议问题