PHP asks for a regular expression: keep the letters, numbers, underscores, decimal points, and dashes in the string.
How to write the regular, thank you ~ ~
Reply content:
PHP asks for a regular expression: keep the letters, numbers, underscores, decimal points, and dashes in the string.
How to write the regular, thank you ~ ~
Output:
string(15) "AB12@#$%()_.-cd"string(9) "AB12_.-cd"
A mistake in correcting your problem first, simple regular expressions are not able to preserve or remove anything. The function of a regular expression is to match the content that matches the condition.
If you simply want to extract the characters that meet the requirements, try the following code:
If you want to ensure that only the above conditions are allowed in a string, you can search the Internet, the demand is still many (user name filter), or use the following match (not tested):
$preg = '/([a-zA-Z0-9.\-_]*?)/';
It's too easy to mention. It's a swarmed problem to think of.
An expression is not difficult to write: [a-zA-Z0-9_\-\.]+
.
But on the mechanism, the regular expression must only find a match at a time. Even in the regular interior can be divided into matching groups, that can only be individually matched to the group extraction, can never be "taken out is a good connection." Can't expect everything to one step.
So you have to find all the matches of the small block expression and then join them.
$patt = '/[a-zA-Z0-9_\\-\\.]+/';preg_match_all($patt, $content, $result);echo implode('', $result);