searls03 Posted October 26, 2012 Share Posted October 26, 2012 please look at the photo to see what I am replacing. I am trying to replace it with either a blank or a line break. <?php $needle='</tbody> </table> </td> <td> <table border="0" align="left"> <tbody>'; $content= str_replace ($needle, '<br> ', $content); echo $content; ?> anytime I try this though, nothing is happening(I am sure there is a good reason), but if I try a single line, it works fine....any help? Quote Link to comment https://forums.phpfreaks.com/topic/269924-php-replace/ Share on other sites More sharing options...
Christian F. Posted October 26, 2012 Share Posted October 26, 2012 I suspect that there are some newline (or whitespace) issues stopping you, more specifically: One of those strings have Windows-style newlines, and the other not. I'd use Regular Expressions for this, to be certain that you handle the newlines and other whitespace, regardless of their exact position and/or count. Adding [\r\n\s]* instead of the newlines, between the tags, as well as a couple of delimiters, and you should be golden. Quote Link to comment https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1387879 Share on other sites More sharing options...
searls03 Posted October 26, 2012 Author Share Posted October 26, 2012 like this? <?php $needle='</tbody>[\r\n\s]* </table>[\r\n\s]* </td>[\r\n\s]* <td>[\r\n\s]* <table border="0" align="left">[\r\n\s]* <tbody>'; $content= str_replace ($needle, '<br> ', $content); echo $content; ?> Quote Link to comment https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1387891 Share on other sites More sharing options...
Christian F. Posted October 26, 2012 Share Posted October 26, 2012 (edited) First I think you should head over to http://regular-exp<b></b>ressions.info, and read up on how RegExps work. They are a powerful tool, but require a lot of knowledge to use properly. You also have to use specific functions for them, as str_replace () doesn't support RegExps. Instead you should use preg_replace (), and it is quite nicely explained in the PHP manual how to use it. Now, according to my post above there are two things you forgot in your edit. First one is the delimiters, which is understandable since you don't know RegExps. The other, however, was to replace the newlines with the character group. (Thus the use of "instead of". ) That means that the RegExp should look like this: $needle = '#[\\n\\r\\s]*</tbody>[\\n\\r\\s]*</table>[\\n\\r\\s]*</td>[\\n\\r\\s]*<td>[\\n\\r\\s]*<table\\s+border="0"\\s+align="left">[\\n\\r\\s]*</tbody>#'; In this example the hash signs (#) are the delimiters. Edited October 26, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1387896 Share on other sites More sharing options...
jcbones Posted October 26, 2012 Share Posted October 26, 2012 If you go with the regex route, don't forget to change the function to preg_replace. Quote Link to comment https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1388024 Share on other sites More sharing options...
searls03 Posted October 27, 2012 Author Share Posted October 27, 2012 sorry to bother, but I don't really get the regex at all, so I am not really sure how to use it....could you help me out with it? Quote Link to comment https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1388113 Share on other sites More sharing options...
searls03 Posted October 27, 2012 Author Share Posted October 27, 2012 (edited) let me explain what my goal is, maybe there is a better way to do this(i am sure there is). I have two tables side by side on my full website, this is saved in a database. I then echo the content onto both my mobile and desktop website. on my desktop site, it is fine, on my mobile website the second table is chopped off. my goal is to instead of ending the first table and starting the second table, I get ride of the end and start and make it one table all in itself using some kind of replacement method. here is the exact markup of how it is shown in the database: <td>General Repairs</td> </tr> </tbody> </table> </td> <td> <table border="0" align="left"> <tbody> <tr> and I want it to look like this when finished: <td>General Repairs</td> </tr> <tr> etc... Edited October 27, 2012 by searls03 Quote Link to comment https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1388115 Share on other sites More sharing options...
Psycho Posted October 27, 2012 Share Posted October 27, 2012 (edited) I'm guessing the problem is that there may be white-space characters (spaces, tabs etc.) that you are not accounting for. Try this $pattern = "#</tbody>[^<]*</table>[^<]*</td>[^<]*<td>[^<]*<table[^>]*>[^<]*<tbody>#is"; $content = preg_replace($pattern, "", $content); Edited October 27, 2012 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1388150 Share on other sites More sharing options...
searls03 Posted October 27, 2012 Author Share Posted October 27, 2012 perfect! thank you! Quote Link to comment https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1388182 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.