Question

Regex:([a-zA-Z0-9]+)@((?1))

Test string: abc def@abc

result:

Match 1: def
Match 2: abc

How to allow spaces in regex?
I want the result to be:

Match 1: abc def
Match 2: abc
Was it helpful?

Solution

You want to add the space character inside your character class [] in order to be matched also.

$text = 'abc def@abc';

preg_match_all('/[a-z0-9 ]+/i', $text, $matches);
print_r($matches[0]);

Output

Array
(
    [0] => abc def
    [1] => abc
)

OTHER TIPS

try

preg_match("/^([a-zA-Z0-9 ])+$/i", $str)

want @ also match try

preg_match("/^([a-zA-Z0-9@ ])+$/i", $str) (i haven't test this)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top