Showing posts with label preg_match(). Show all posts
Showing posts with label preg_match(). Show all posts
PHP preg_replace Example
Posted by Raj
PHP preg_replace Example
preg_replace: Perform a regular expression search and replace.PHP preg_replace() returns an array if the subject parameter is an array, or a string otherwise.
Example 1:
<?php
$string = 'Hello,How r u?';
$string = preg_replace('/\s\s+/', ' ', $string);
echo $string;
?>
Output:
"Hello,How r u?"
Example 2:
<?php
$string = '100 Rs 50';
$pattern = '/(\d+).(\w+) (\d+)/i';
$replacement = '${1}.$3 $2';
echo preg_replace($pattern, $replacement, $string);
?>
Output:
100.50 Rs
preg_replace: Perform a regular expression search and replace.PHP preg_replace() returns an array if the subject parameter is an array, or a string otherwise.
Example 1:
<?php
$string = 'Hello,How r u?';
$string = preg_replace('/\s\s+/', ' ', $string);
echo $string;
?>
Output:
"Hello,How r u?"
Example 2:
<?php
$string = '100 Rs 50';
$pattern = '/(\d+).(\w+) (\d+)/i';
$replacement = '${1}.$3 $2';
echo preg_replace($pattern, $replacement, $string);
?>
Output:
100.50 Rs