arathi Posted October 17, 2007 Share Posted October 17, 2007 Hi I want to check whether a string contains HTML elements. Is there is any function to check whether a string contains HTML elements. Link to comment https://forums.phpfreaks.com/topic/73643-check-whether-a-string-contains-html-elements/ Share on other sites More sharing options...
ToonMariner Posted October 17, 2007 Share Posted October 17, 2007 you could compare the string with itself after tags have been removed if they are identical then no html is in teh string if (strcmp($string, strip_tags($string)) == 0) { // no tags found } else { // tags found } Link to comment https://forums.phpfreaks.com/topic/73643-check-whether-a-string-contains-html-elements/#findComment-371510 Share on other sites More sharing options...
ajaylulia Posted October 17, 2007 Share Posted October 17, 2007 You can also use Regular Expression, following is an example for the same. <? $str = "<p> helo </p><b>je</b>"; echo $str; echo preg_match ('/<[^>]*>/', $str); ?> Link to comment https://forums.phpfreaks.com/topic/73643-check-whether-a-string-contains-html-elements/#findComment-371514 Share on other sites More sharing options...
BlueSkyIS Posted October 17, 2007 Share Posted October 17, 2007 ^^ except this would also strip non-HTML. i suggest use of the first method over the second. Link to comment https://forums.phpfreaks.com/topic/73643-check-whether-a-string-contains-html-elements/#findComment-371553 Share on other sites More sharing options...
effigy Posted October 17, 2007 Share Posted October 17, 2007 ^^ except this would also strip non-HTML. i suggest use of the first method over the second. preg_match does not change the string. Link to comment https://forums.phpfreaks.com/topic/73643-check-whether-a-string-contains-html-elements/#findComment-371557 Share on other sites More sharing options...
BlueSkyIS Posted October 17, 2007 Share Posted October 17, 2007 true. what i meant was that the preg_match as written would match entities that are not necessarily HTML. conversely, strip_tags is meant to match only HTML tags. Link to comment https://forums.phpfreaks.com/topic/73643-check-whether-a-string-contains-html-elements/#findComment-371561 Share on other sites More sharing options...
ajaylulia Posted October 17, 2007 Share Posted October 17, 2007 correct, preg_match will only match the string however, preg_replace will do the magic. Use Following code to remove the html links: <? $str = "<p> helo </p><b>je</b>"; echo $str; echo preg_replace ('/<[^>]*>/', '', $str); ?> Link to comment https://forums.phpfreaks.com/topic/73643-check-whether-a-string-contains-html-elements/#findComment-371566 Share on other sites More sharing options...
BlueSkyIS Posted October 17, 2007 Share Posted October 17, 2007 yes, but i repeat. That code will also match NON-HTML characters. For instance: <[email protected]> or <XML> or <something else which isn't HTML> Link to comment https://forums.phpfreaks.com/topic/73643-check-whether-a-string-contains-html-elements/#findComment-371580 Share on other sites More sharing options...
ToonMariner Posted October 17, 2007 Share Posted October 17, 2007 strip tags removes php and html - if the grey matter serves... Link to comment https://forums.phpfreaks.com/topic/73643-check-whether-a-string-contains-html-elements/#findComment-371935 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.