PHP: how to split a line on a dash and everything between brackets. (preg_split or preg_match)

I have been wrapping my head around this for several days, but nothing gives 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 managed to get all this using preg_match_all, but then Dash-Bound-Word broke. Trying to compare it with the surrounding spaces did not work, as this would break all the words except those attached to them.

The preg_match_all statement used (which also broke the words associated with the dash):

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

Of course, I'm not a specialist on preg_match, preg_split, so any help here would be very appreciated.

+4
source share
4

preg_match_all:

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

  • \w+ - 1 -
  • (?:[- ]\w+)* - 0 ...
    • [- ] - ( \s, )
    • \w+ - 1 -

IDEONE:

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

:

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

:

/\s*(?<!\w(?=.\w))[\-[\]()]\s*/

:

  • [\-[\]()] ( ). char .
  • lookbehind (?<!\w) : " ".
  • lookahead (?=.\w), : " , char - , ".
  • \s* - .

:

$input_line = "Some Words - Other Words (More Words) Dash-Binded-Word";
$result = preg_split("/\s*(?<!\w(?=.\w))[\-[\]()]\s*/", $input_line);
var_dump($result);

:

array(4) {
  [0]=>
  string(10) "Some Words"
  [1]=>
  string(11) "Other Words"
  [2]=>
  string(10) "More Words"
  [3]=>
  string(16) "Dash-Binded-Word"
}

parens

, :

$result = preg_split("/\s*(?:(?<!\w)-(?!\w)|(\(.*?\)|\[.*?]))\s*/", $input_line, -1, PREG_SPLIT_DELIM_CAPTURE);
+2

( str_replace ). , :

$var = "Some Words - Other Words (More Words) Dash-Binded-Word";
$arr = Array(" - ", " (", ") ");
$var2 = str_replace($arr, "|", $var);
$final = explode('|', $var2);
var_dump($final);

:

array (4) {[0] = > string (10) "Some Words" [1] = > string (11) "Other " [2] = > string (10) "More Words" [3] = > string (16) "Dash-Binded-Word" }

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

$var=preg_replace('/[^A-Za-z\-]/', ' ', $var);
$var=str_replace('-', ' ', $var); // Replaces all hyphens with spaces.
print_r (explode(" ",preg_replace('!\s+!', ' ', $var)));  //replaces all multiple spaces with one and explode creates array split where there is space

: -

Array ( [0] => Some [1] => Words [2] => Other [3] => Words [4] => More [5] => Words [6] => Dash [7] => Binded [8] => Word ) 
0

Source: https://habr.com/ru/post/1607716/


All Articles