Search code examples
phpregexpreg-matchpreg-match-allpreg-split

PHP: How to split a string by dash and everything between brackets. (preg_split or preg_match)


I've been wrapping my head around this for days now, but nothing seems to give the desired result.

Example:

$var = "Some Words - Other Words (More Words) Dash-Binded-Word";

Desired result:

array(
[0] => Some Words
[1] => Other Words
[2] => More Words
[3] => Dash-Bound-Word
)

I was able to get this all working using preg_match_all, but then the "Dash-Bound-Word" was broken up as well. Trying to match it with surrounding spaces didn't work as it would break all the words except the dash bound ones.

The preg_match_all statement I used (which broke up the dash bound words too) is this:

preg_match_all('#\(.*?\)|\[.*?\]|[^?!\-|\(|\[]+#', $var, $array);

I'm certainly no expert on preg_match, preg_split so any help here would be greatly appreciated.


Solution

  • You can use a simple preg_match_all:

    \w+(?:[- ]\w+)*
    

    See demo

    • \w+ - 1 or more alphanumeric or underscore
    • (?:[- ]\w+)* - 0 or more sequences of...
      • [- ] - a hyphen or space (you may change space to \s to match any whitespace)
      • \w+ - 1 or more alphanumeric or underscore

    IDEONE demo:

    $re = '/\w+(?:[- ]\w+)*/'; 
    $str = "Some Words - Other Words (More Words) Dash-Binded-Word"; 
    preg_match_all($re, $str, $matches);
    print_r($matches[0]);
    

    Result:

    Array
    (
        [0] => Some Words
        [1] => Other Words
        [2] => More Words
        [3] => Dash-Binded-Word
    )