RE: [PHP] preg_replace()

From: Date: Mon, 14 Apr 2003 16:33:34 +0000
Subject: RE: [PHP] preg_replace()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-143803@lists.php.net to get a copy of this message
ok, as usual, the details matter...can you ever have cell2 like you typed? is html ALWAYS going to be that simple? here's a poor solution given the lack of details... <?php $mytable = "<table> <tr> <td>cell 1</td> <td>cell 2</td> </tr> </table>"; $pattern = "/<td>cell (\d)/i"; $replacement = "<td id=$1>cell $1"; print preg_replace($pattern, $replacement, $mytable); ?> gives the output <table> <tr> <td id=1>cell 1</td> <td id=2>cell 2</td> </tr> </table> if you actually can have cell2 and want to keep it like that make the following changes: $pattern = "/<td>cell(.*\d)/i"; $replacement = "<td id=$1>cell$1"; i feel like this is a pretty janky solution, but given the information presented, i can't offer much better... > 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... > > Any thoughts? > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.general (#143803) next »