-2

So I'm working on some php, and looking for some help with regular exp.

I have an array of string looking like this:

$variable_with_strings
array(
    [0][art] => >AngusDelu -
    [1][art] => >PatatasJr -
    [2][art] => >AngusHabane *
    [3][art] => >Papas Med *
    [4][art] => >AngusDelu*
    [5][art] => >Papas Med*
    [6][art] => >AngusHabane**
    [7][art] => >Papas Med**
    [8][art] => >Papas Med_*-
    [9][art] => >Manza Med_*-
)

I loop tru each one, treating as single variable ($variable_with_strings

And I want to split the string in two for later group it

something looking for a regex with preg_split() like this: (I need the two parts; 1.-the description item and 2.- the group signs)

for each one I expect to have:

$word = array
    [art] => >AngusDelu
    [sep] =>  -)

$word = array(
    [art] => >PatatasJr
    [sep] =>  -)

$word = array(
    [art] => >AngusHabane
    [sep] =>  *)

$word = array(
    [art] => >Papas Med
    [sep] =>  *)

$word = array(
    [art] => >AngusDelu
    [sep] => *)

$word = array(
    [art] => >Papas Med
    [sep] => *)

$word = array(
    [art] => >AngusHabane
    [sep] => **)

$word = array(    
    [art] => >Papas Med
    [sep] => **)

$word = array(
    [art] => >Papas Med
    [sep] => _*-)

$word = array(
    [art] => >Manza Med
    [sep] => _*-)
)

So I was using something like:

$word = preg_split('/[^\w]/',$variable_with_strings);

But this splits every white space,

This is kinda tricky, I guess, because in the first character there is a > always, and the last characters are *, _, - or some time a space before these " ", and some times the special chars come in 1, 2 or even 3 at the end.

can this be done this way?

thanks

2 Answers2

0

I would just use

[^a-z]+$

with preg_replace and the m and i modifiers. (Your examples don't have numbers; if you want numbers as well add \d in the character class)

Regex101 demo: https://regex101.com/r/rX3oK7/2 (note the g modifier on the demo isn't used in PHP, the preg_replace is global already)

PHP Usage:

$string = '[art] => >AngusDelu -
[art] => >PatatasJr -
[art] => >AngusHabane *
[art] => >Papas Med *
[art] => >AngusDelu*
[art] => >Papas Med*
[art] => >AngusHabane**
[art] => >Papas Med**
[art] => >Papas Med--
[art] => >Manza Med--';
echo preg_replace('/[^a-z]+$/im', '', $string);

Output:

[art] => >AngusDelu
[art] => >PatatasJr
[art] => >AngusHabane
[art] => >Papas Med
[art] => >AngusDelu
[art] => >Papas Med
[art] => >AngusHabane
[art] => >Papas Med
[art] => >Papas Med
[art] => >Manza Med

PHP Demo: https://eval.in/479444

chris85
  • 23,846
  • 7
  • 34
  • 51
0

You could use preg_split, match just the end of the string like /(\W+$)/ and also pass the PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE flags as the fourth parameter to capture whatever you are splitting on as part of the result. PREG_SPLIT_DELIM_CAPTURE captures the delimeter you are splitting on and PREG_SPLIT_NO_EMPTY will make sure there are no empty results.

Example:

<?php
$art = array(
    '>AngusDelu -',
    '>PatatasJr -',
    '>AngusHabane *',
    '>Papas Med *',
    '>AngusDelu*',
    '>Papas Med*',
    '>AngusHabane**',
    '>Papas Med**',
    '>Papas Med--',
    '>Manza Med--',
    );

foreach($art as $a){
    $split = preg_split('/(\W+$)/', $a, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
    echo '<pre>'.print_r($split,true).'</pre>';
}

Demo: http://codepad.viper-7.com/9mCDlc

Outputs:

Array
(
    [0] => >AngusDelu
    [1] =>  -
)

Array
(
    [0] => >PatatasJr
    [1] =>  -
)

Array
(
    [0] => >AngusHabane
    [1] =>  *
)

Array
(
    [0] => >Papas Med
    [1] =>  *
)

Array
(
    [0] => >AngusDelu
    [1] => *
)

Array
(
    [0] => >Papas Med
    [1] => *
)

Array
(
    [0] => >AngusHabane
    [1] => **
)

Array
(
    [0] => >Papas Med
    [1] => **
)

Array
(
    [0] => >Papas Med
    [1] => --
)

Array
(
    [0] => >Manza Med
    [1] => --
)

Each one is in a separate array because you said that you have an array, so you need to loop over and run split on each entry in the array. You could easily work with one array at a time in the loop or combine them into a single array to work with later.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
  • In fact each one of the orginal is a $var not the whole a single one – Daniel Clarks Dec 02 '15 at 23:07
  • Well, then this would work fine on an individual `$var`. That is essentially what the example does. – Jonathan Kuhn Dec 02 '15 at 23:09
  • this worked fine on individuals $vars, using "0" modificator instead of "-1" since I needed all of the last characters delimited in another $var, thanks, sorry for the poor redaction/slow answer, @chris85 – Daniel Clarks Dec 10 '15 at 21:29
  • I don't understand what you mean. In all cases given, the above would appear to work without issue. And the only place I'm using `-1` is in the limit argument which just means "no limit". The manual specifically states that `A limit of -1, 0 or NULL means "no limit"`, so changing that to `0` would make no difference. – Jonathan Kuhn Dec 10 '15 at 21:34