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
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
php preg_replace,
php preg_replace example,
preg_match(),
preg_replace_callback(),
preg_split()
.You can leave a response, or trackback from your own site.