Automatic line wrap with regex

2

I have the following string: $var = "Saudi Arabia U23 x South Korea U23"; I want to split the names that are separated by "x" , I did the following:

$arr = preg_split('/[v|x]/', $var);

I used% w /% because sometimes the string could come with a "v" separating the names, not the X, the problem is that if you have an "x" or "v" included in the name, x "or the" v "of the split, it will cut too, but I just want to separate the names delimited by "V e X" or " x " . How would I do this in regex?

    
asked by anonymous 07.01.2016 / 18:57

2 answers

3

Just put spaces followed by + outside the brackets:

$arr = preg_split('/ +[v|x] +/', $var);

This regex will remove the "x" , " characters and also those with more than one space such as " x " and "v"

    
07.01.2016 / 19:22
1

This regex solves the problem, it searches for one or more spaces followed by a v or x followed by one or more spaces.

$str = 'abc X edfxct';
$arr = preg_split('/\s+v|x\s+/i', $str);

echo "<pre>";
print_r($arr);

Output:

Array
(
    [0] => abc 
    [1] => edfxct
)
    
07.01.2016 / 19:25