Domanda

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
È stato utile?

Soluzione

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
)

Altri suggerimenti

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)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top