We are using this code to determine if a string has special characters.
$string = 'some test';
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_ ¬-]/', $string))
{
echo "Special characters found!";
}
How can we check if the spacial character is inside or center of the string only?
Any help is appreciated.
CodePudding user response:
The following will match a non-space character (\S
) followed by one or more space characters (\s
), followed by another non-space character:
\S\s \S
This should serve your purpose if you only want to find out whether there are space characters 'inside' the string. If you want to capture those space characters (to carry out substitution, etc.), you should put the \s
part inside a capturing group:
\S(\s )\S
EDIT: I just realised you meant to say 'special' not 'spacial'!
This depends on your definition of 'inside' - are you expecting letters before/after, or would you include space characters, etc.? If you are looking for special characters enclosed by letters, your regex would be:
[a-zA-Z][\'^£$%&*()}{@#~?><>,|=_ ¬-] [a-zA-Z]
Again, you can use a capturing group to get at those special characters:
[a-zA-Z]([\'^£$%&*()}{@#~?><>,|=_ ¬-] )[a-zA-Z]