views:

47

answers:

2

Dear all, I have trouble again with preg_replace again. I terrible need a book for preg_replace

my intention is to replace <td> author </td> with <td> Level1 </td>

Any ideas? the <td> author </td> is wrapped with other HTML element

<td align="center">
  <a onclick="return listItemTask('cb1','block')" href="javascript:void(0);">
    <img width="16" height="16" border="0" alt="Blocked" src="images/tick.png">
  </a>
</td>
<td>
  Author
</td>
+2  A: 

From Perl regular expression manual page: (or PHP PCRE docs)

Modifiers

Matching operations can have various modifiers. Modifiers that relate to the interpretation of the regular expression inside are listed below.

  • s: Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.
jpalecek
Since this is PHP, why not quote [the PHP doc](http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php)?
KennyTM
@KennyTM: Because I searched for `preg_replace` and the syntax is not described there. AFAIK, PCRE should be totally compatible with Perl's regexp. I'll incorporate the link in my answer.
jpalecek
A: 

A little code never hurts...

<?php
$string = '
<td align="center">
  <a onclick="return listItemTask(\'cb1\',\'block\')" href="javascript:void(0);">
    <img width="16" height="16" border="0" alt="Blocked" src="images/tick.png">
  </a>
</td>
<td>
  Author
</td>';

$string = preg_replace("/<td>.*Author.*<\/td>/s", "teste", $string);

echo $string;

?>

Do notice the \s modifier used to allow . to match newlines.

Frankie
bow head! bow head!
tweakmy
@tweakmy I really couldn't tell by your comment if this is what you were looking for or if [you'r just playing](http://www.urbandictionary.com/define.php?term=bow%20head "definition for bow head") but, if this is what you'r looking for, you should click on the 'V' next to the question (accept it). That will give both of us credit for it! Welcome to StackOverflow (SO)!
Frankie