Jump to content

Recommended Posts

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?

post-108372-0-79353300-1351221746_thumb.png

Link to comment
https://forums.phpfreaks.com/topic/269924-php-replace/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1387879
Share on other sites

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 by Christian F.
Link to comment
https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1387896
Share on other sites

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 by searls03
Link to comment
https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1388115
Share on other sites

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 by Psycho
Link to comment
https://forums.phpfreaks.com/topic/269924-php-replace/#findComment-1388150
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.