How to do a regex replace in PHP
26 August 2024 (Updated 26 August 2024)
Syntax:
preg_replace(
string|array $pattern,
string|array $replacement,
string|array $subject
): string|array|null
Example (when passed a string):
function removeNonLetters($str) {
return preg_replace(
'/[^A-Za-z]/',
'',
$str
);
}
echo removeNonLetters('sa.j.a--d') // => 'sajad'
Sources/links
Tagged:
PHP