Jump to content

preg_replace


lindm

Recommended Posts

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);

 

Link to comment
https://forums.phpfreaks.com/topic/190341-preg_replace/
Share on other sites

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);

 

Link to comment
https://forums.phpfreaks.com/topic/190341-preg_replace/#findComment-1004392
Share on other sites

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.