0

I am doing a preg_replace on a string and the replacement contains a count variable that needs to be placed inbetween the pattern. But I am having difficulty doing that as the count is taken as a pattern and not as a count variable. Here is my code.


$count = 0;
$result_stringXa = "<select>
<option value = 'lb1'>ListItem1</option>
<option value = 'lb2'>ListItem2</option>
<option value = 'lb3'>ListItem3</option>
</select>"

if (preg_match('<select>', $result_stringXa))
{
$count++;
}
$string = $result_stringXa;
$pattern = '<select>';
$replacement = '< select \'lb\$count\'>';
echo preg_replace($pattern, $replacement, $string);
}

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by vibhaJ
0
<?
	$count = 0;
$result_stringXa = "<select>
<option value = 'lb1'>ListItem1</option>
<option value = 'lb2'>ListItem2</option>
<option value = 'lb3'>ListItem3</option>
</select>";

if(preg_match('<select>', $result_stringXa))
{
	$count++;
}
$string = $result_stringXa;
$pattern = '/<select>/';
$replacement = "<select 'lb$count'>";
echo preg_replace($pattern, $replacement, $string);

?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.