<?php $originalString = "Pritom K Mondal, 0154785"; function ord2($s2) { return "&#" . ord($s2) . ";"; } echo preg_replace("/[^a-zA-Z0-9]/e", "ord2('\\0')", $originalString) ?>
And output will be as:
Pritom K Mondal, 0154785
<?php $originalString = "Pritom K Mondal, 0154785"; function ord2($s2) { return "&#" . ord($s2) . ";"; } echo preg_replace("/[^a-zA-Z0-9]/e", "ord2('\\0')", $originalString) ?>
Pritom K Mondal, 0154785
String cardNumber = "4444 3333 2222 111"); cardNumber = cardNumber.trim(); cardNumber = cardNumber.replaceAll("\\D+", ""); Output: 4444333322221111
<?php
$string = 'April 15, 2003';$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);?>
April1,200
<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);?>
The bear black slow jumped over the lazy dog.
<?php
$str = 'foo o';$str = preg_replace('/\s\s+/', ' ', $str);// This will be 'foo o' nowecho $str;?>
count
parameter<?php
$count = 0;
echo preg_replace(array('/\d/', '/\s/'), '*', 'xp 4 to', -1 , $count);
echo $count; //3?>
xp***to 3
Only receive - letters, numbers, spaces
$str = preg_replace("/[^A-Za-z0-9 ]/","",$str);
$str = preg_replace("/[^A-Za-z0-9\s]/","",$str);
$str = preg_replace("/[^A-Za-z0-9[:space:]]/","",$str);
$str
= preg_replace("/([^a-z0-9A-Z[:space:]\'\"\?\!\,\.\-\:\~]+)/", "",
]);
$str
$str
= preg_replace("/([--]+)/", "-", $str);
$
= preg_replace("/<\/address>\r\n/e", "'</address>'", $
str
);
str
When using the /e modifier in preg_replace, you have to pass a string of code to
be evaluated as the replacement parameter, not an already-evaluated expression.
$= preg_replace("/<\/address>[[:space:]]*
str
?
<address/e", "'</address><address'", $);
str