preg_match unknown modifier '|' (pipe)
Posted: Mon Sep 21, 2009 10:31 pm
I'm trying to write a method that checks for special characters in a given string, however it says that the pipe character is an unknown modifier
The created regex:
Code: Select all
public static function hasSpecialCharacters($string) {
// checking for special characters
$specialChars = array('!', '\@', '#', '\\\\', '/', '\^', '\+',
'\(', '\)', '\[', '\]', '\.', ',', '\$', '\?',
'<', '>', '{', '}', '\|', '-', '=', '~', '\`' );
$regex ='/[';
$last = end($specialChars);
foreach($specialChars as $char) {
$regex .= $char;
if($char != $last)
$regex .= '|';
}
$regex .=']/';
if(preg_match($regex, $string)) {
return true;
}
return false;
}Code: Select all
/[!|\@|#|\\|/|\^|\+|\(|\)|\[|\]|\.|,|\$|\?|<|>|{|}|\||-|=|~|\`]/