I have multiple strings with same curly braces I want to replace them as dynamic if I get the count as 1 then need to replace the first occurrence, If count as 2 then replaces t
You can greatly simplify your code by fetching all the replacement values in one query:
$String = "{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}";
if(preg_match_all("/\{\{[^{}]+\}\}/", $String, $matches)) {
$Query = "SELECT linkVal, link FROM student WHERE linkVal IN('".implode("','", $matches[0])."')";
$Result = $con->query($Query);
if ($rows = $Result->fetchAll(PDO::FETCH_ASSOC)) {
$NewValue = str_replace(array_column($rows, 'linkVal'), array_column($rows, 'link'), $String);
}
echo json_encode($NewValue);
}