matthewtbaker Posted October 5, 2012 Share Posted October 5, 2012 Hi, I've been trying to remove a suborn <p> </p> which sometimes appears at the bottom of a database entry after the CMS in use strips out 'unsafe' HTML. The value returned from my SQL string is: (8, 'Title', 'Alias', '', '<p>My Paragraph</p>\r\n<p> </p>', '', 1); I've tried removing the unwanted <p> </p> using the code and find variations below, but still no success. $myIntro= $db->intro; $find = array('<p></p>','<p> </p>','<p> </p>',' ','\n'); $myIntro = str_ireplace($find,'', $myIntro); Does anyone have any other approaches to try and strip this out from my string? I can't dump all <p> tags as I still need the rest for formatting. Many thanks Matt Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/ Share on other sites More sharing options...
seanlim Posted October 5, 2012 Share Posted October 5, 2012 What do you mean "still no success"? Your code above seems to be able to strip out the unwanted tags in your example. What's your criteria for "success"? Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1382848 Share on other sites More sharing options...
matthewtbaker Posted October 5, 2012 Author Share Posted October 5, 2012 What do you mean "still no success"? Your code above seems to be able to strip out the unwanted tags in your example. What's your criteria for "success"? Hi seanlim, Even though I run the code (above) the unwanted '<p> </p>' is still present in the variable and echoed to my page. It interferes with layout and alignment. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1382855 Share on other sites More sharing options...
drisate Posted October 5, 2012 Share Posted October 5, 2012 You can strip out HTML of your string using $string = strip_tags($myIntro); or you can str_replace one tag at the time $find = array('<p>', '</p>'); Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1382862 Share on other sites More sharing options...
matthewtbaker Posted October 5, 2012 Author Share Posted October 5, 2012 You can strip out HTML of your string using $string = strip_tags($myIntro); or you can str_replace one tag at the time $find = array('<p>', '</p>'); Thanks drisate, Unfortunately that solution will not work as I still need to keep the other <p> tags for correct formatting. I only wish to target the <p> tags with a blank space. Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1382867 Share on other sites More sharing options...
Jessica Posted October 5, 2012 Share Posted October 5, 2012 <?php $myIntro= '<p>My Paragraph</p>\r\n<p> </p>'; $myIntro = rtrim($myIntro, '<p> </p>'); echo $myIntro; <p>My Paragraph</p>\r\n Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1382890 Share on other sites More sharing options...
matthewtbaker Posted October 5, 2012 Author Share Posted October 5, 2012 <?php $myIntro= '<p>My Paragraph</p>\r\n<p> </p>'; $myIntro = rtrim($myIntro, '<p> </p>'); echo $myIntro; Thanks Jessica. Unfortunately it did not work. I'm thinking that maybe the " " is not actually white space but hidden characters stored in the db?... Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1382894 Share on other sites More sharing options...
Jessica Posted October 5, 2012 Share Posted October 5, 2012 It works for that string. Do a var_dump on the string. Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1382898 Share on other sites More sharing options...
matthewtbaker Posted October 5, 2012 Author Share Posted October 5, 2012 (edited) It works for that string. Do a var_dump on the string. Did a var_dump and the <p> </p> is still present. Most frustrating. <td><p>My Paragraph </p><p> </p></td> Edited October 5, 2012 by matthewtbaker Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1382906 Share on other sites More sharing options...
Jessica Posted October 5, 2012 Share Posted October 5, 2012 *eyeroll*. Do a var_dump and post the contents of it. Var_dump is not to remove strings. The biggest problem is that you apparently don't see a difference between "<td><p>My Paragraph </p><p> </p></td>" AND "<p>My Paragraph</p>\r\n<p> </p>" Which IS the real string you're working with? God. Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1382915 Share on other sites More sharing options...
matthewtbaker Posted October 8, 2012 Author Share Posted October 8, 2012 Thanks Jessica. I am perfectly aware of var_dump. In this case it does not prove useful as the character which is held within the <p> </p> does not render. I have since done further investigation into the mystery space and it is clear it is not actually 'white space' so I will work on another method to remove it. Allah. Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1383649 Share on other sites More sharing options...
kicken Posted October 8, 2012 Share Posted October 8, 2012 var_dump(bin2hex($string)); That will give you the string encoded as hexdecimal digits, so you can see what exactly is between those p tags. Quote Link to comment https://forums.phpfreaks.com/topic/269108-no-success-in-removing/#findComment-1383681 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.