Jump to content

[SOLVED] can't preg_replace this string?


ballhogjoni

Recommended Posts

It seems that I can't replace this string:

 

<tr><td height="290" colspan="3"></td></tr><tr><td height="163" valign="top" colspan="3"></td></tr>

 

this is my preg_replace()

 

$sReplaceHTML = preg_replace( '%<tr><td height="290" colspan="3"></td></tr><tr><td height="163" valign="top" colspan="3"></td></tr>%','',$sStripDOC );

 

what am I doing wrong? Yes the code contains the pattern I want to replace.

Link to comment
Share on other sites

<?php
$sStripDOC = '<tr><td height="290" colspan="3"></td></tr><tr><td height="163" valign="top" colspan="3"></td></tr>';
$sReplaceHTML = preg_replace( '%<tr><td height="290" colspan="3"></td></tr><tr><td height="163" valign="top" colspan="3"></td></tr>%','',$sStripDOC );

var_dump($sReplaceHTML);

//outputs string(0) "" 
?>

 

The entire string was found, and replaced by '' which you specified in parameter 2 of preg_replace.  It is working as intended.

Link to comment
Share on other sites

I think the problem is that there is whitespace in the string that i didn't include before.

 

Here is what the string really looks like:

<tr> 
  <td height="290" colspan="3"> 
    
  </td>
</tr>
<tr> 
  <td height="163" valign="top" colspan="3"> 
    
  </td>
</tr>

 

How do you get rid of whitespace?

Link to comment
Share on other sites

I just posted the results of my struggles with preg_ functions at http://www.phpfreaks.com/forums/index.php/topic,218141.0.html

 

You should find that somewhat instructive. Also, with regards to any possible white spaces, your script should look more like this:

preg_replace( '/<tr>\s?<td height="290" colspan="3">\s?</td>\s?</tr>\s?<tr>\s?<td height="163" valign="top" colspan="3">\s?</td>\s?</tr>/','',$sStripDOC )

 

I'm no expert, but I think that should help you somewhat. BTW, what's with the '%' at the start and end of your preg pattern?

Link to comment
Share on other sites

thanks, the % is the delimeter. I know of three different delimeters. You can use them as long as the delimeter is the same as the end. IE..

/your pattern/ or #your pattern# or %your pattern%

 

Whats nice about using #your pattern# or %your pattern% is that you don't have to escape the forwardslash in your pattern.

Link to comment
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.