xxxxxxxxxx
$text = preg_replace_callback(
"/\\[\\[([^|]+\\|)(.*?)\\]\\]/",
[ $this, 'linkReplace' ],
$text
);
$text = preg_replace( "/<\/?[^>]+>/", "", $text );
return $text;
xxxxxxxxxx
<?php
$str = "1R6uS7#b8N*dV2q4G9E0H!xYcMzW\$L37KtR3xL2vFg9nD4mH0jP6sQwX8zVb1cN5";
echo preg_replace("/[^0-9a-zA-Z]/",'',$str);
echo PHP_EOL;
echo preg_replace("/[^0-9]/",'',$str);
echo PHP_EOL;
$str = "1R6uS7#b8N*d V2q4G9E0H !xYcMzW\$L37KtR3xL2vFg9nD4m H0jP6sQwX8zVb1cN5";
echo preg_replace('/\s+/', '', $str);#replace spaces from string
?>
xxxxxxxxxx
<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
xxxxxxxxxx
$result = preg_replace('/abc/', 'def', $string); # Replace all 'abc' with 'def'
$result = preg_replace('/abc/i', 'def', $string); # Replace with case insensitive matching
$result = preg_replace('/\s+/', '', $string); # Strip all whitespaces
xxxxxxxxxx
preg_replace($pattern, $replacement, $subject [, $limit [, &$count]]);
// Returns an array if the subject parameter is an array,
// or a string otherwise.
// If matches are found, the new subject will be returned, otherwise
// subject will be returned unchanged or NULL if an error occurred.
xxxxxxxxxx
<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
?>
xxxxxxxxxx
$result = preg_replace(
array('/pattern1/', '/pattern2/'),
array('replace1', 'replace2'),
$string
);
xxxxxxxxxx
$result = preg_replace('/abc(def)hij/', '/\\1/', $string);
$result = preg_replace('/abc(def)hij/', '/$1/', $string);
$result = preg_replace('/abc(def)hij/', '/${1}/', $string);
xxxxxxxxxx
<!DOCTYPE html>
<html>
<body>
<?php
// Display result after replace and count
echo preg_replace(array('/\d/', '/\s/'),
'*', '1234567890', 8);
?>
</body>
</html>
xxxxxxxxxx
# Strip HTML tag
$result = preg_replace('#<span id="15">.*</span>#m', '', $string);