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
https://forums.phpfreaks.com/topic/125895-solved-cant-preg_replace-this-string/
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.

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?

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.