RE: [PHP] preg_replace()
| From: | John W. Holmes | Date: | Tue, 15 Apr 2003 02:05:27 +0000 |
| Subject: | RE: [PHP] preg_replace() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-143861@lists.php.net to get a copy of this message | ||
> I have a table in which I want to add an ID to each cell, using
> preg_replace.
>
> $mytable = "<table>
> <tr>
> <td>cell 1</td>
> <td>cell2</td>
> </tr>
> </table>";
> echo preg_replace("/<td>/i", "<td id=number>",$mytable);
>
> The output I want is this:
> <table>
> <tr>
> <td id=1>cell 1</td>
> <td id=2>cell2</td>
> </tr>
> </table>
>
> Can this be done?
> I have been frying my brain with this now for two days...
This works (assumes there's only one table):
$test = "<table>
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
</table>";
$x = 1;
$test2 = preg_replace("/<td>/e",'sprintf("<td
id=%d>",$x++)',$test);
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/