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. Quote Link to comment 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 } Quote Link to comment 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); ?> Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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); ?> Quote Link to comment 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: <someemail@someaddress.com> or <XML> or <something else which isn't HTML> Quote Link to comment 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.