Menus

Showing posts with label php preg_replace. Show all posts
Showing posts with label php preg_replace. Show all posts

Saturday 5 January 2019

Replace First Occurrence of String With PHP


using preg_replace.

preg_replace — Perform a regular expression search and replace

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [,int &$count ]] )


Example

preg_replace('/nothing/', 'something', $str, 1);

Example


<?php

$str="Dear Parent, Fee dues of Mr {V} is {V} on {V}";
echo $str ."<br>";
$c='Shaneesh';
$str= preg_replace("/{V}/", $c, $str, 1);
$str= preg_replace("/{V}/", '2500', $str, 1);
$str= preg_replace("/{V}/", '10-12-2019', $str, 1);
echo $str."<br>";

?>

Output

Dear Parent, Fee dues of Mr {V} is {V} on {V}
Dear Parent, Fee dues of Mr Shaneesh is 2500 on 10-12-2019