lindm Posted January 30, 2010 Share Posted January 30, 2010 Have a html code where I want to replace certain text fields with a new string as below. My problem is that the replacement should extract the id and class from the matching string to be used in the replacement. Any suggestions? <html> <body> <table> <tr> <td><input name="KRRfield1" type="text" class="fb" id="KRRfield1" value="" /></td> <td><input name="KRRfield2" type="text" class="fb fy" id="KRRfield2" value="" /></td> <td><input name="RRfield3" type="text" class="fb" id="RRfield3" value="" /></td> <td><input name="RRfield4" type="text" class="fb fy" id="RRfield4" value="" /></td> </tr> </table> </body> </html>'; $pattern='/<input name="KRR.+\/>/'; $replacement="'.field(fieldid,class).'"; echo preg_replace($pattern, $replacement, $string); Quote Link to comment https://forums.phpfreaks.com/topic/190341-preg_replace/ Share on other sites More sharing options...
lindm Posted January 30, 2010 Author Share Posted January 30, 2010 Solved it. $string = ' <html> <body> <table> <tr> <td><input name="KRRfield1" type="text" class="fb" id="KRRfield1" value="" /></td> <td><input name="KRRfield2" type="text" class="fb fy" id="KRRfield2" value="" /></td> <td><input name="RRfield3" type="text" class="fb" id="RRfield3" value="" /></td> <td><input name="RRfield4" type="text" class="fb fy" id="RRfield4" value="" /></td> </tr> </table> </body> </html>'; $pattern='/(<input name="KRR)(.+)(type="text" class=")(.+)(")(.+)(id=")(.+)(" value)(.+)(\/>)/'; //$pattern='/<input name="KRR.+\/>/'; $replacement="'.field($8,$4).'"; echo preg_replace($pattern, $replacement, $string); Quote Link to comment https://forums.phpfreaks.com/topic/190341-preg_replace/#findComment-1004392 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.