Substring starting at the end

1

I need to separate the string, remove the blank spaces and the arrow, I just want to keep the tags, each one in a different variable. The first one if it comes out but the second one no longer.

$str = 'PD/R --> PPA';
$parte_1 = substr($str,0,strrpos($str, "-")-2).'<br>';
$parte_2 = substr($str, -2);

These labels may vary, for example: 'F --> A' ó 'F --> FJ' . I reiterate, I just want to keep the two labels.

Thank you.

    
asked by Alberto Siurob 26.12.2016 в 19:29
source

3 answers

5

Try:

 $tags = explode(' --> ',$str);
 echo $tags[0].", ".$tags[1];

Full reference to explode ()

    
answered by 26.12.2016 / 19:32
source
1

You can use the explode feature to divide your string with reference space. Once this is done, take the first and third positions of the generated array:

$str = 'PD/R --> PPA';
$partes = explode(" ",$str);
$parte1 = $partes[0]; //PD/R
$parte2 = $partes[2]; //PPA
    
answered by 26.12.2016 в 19:34
0

One more example, but this time with preg_split :

$string = 'PD/R --> PPA';
$arrayString = preg_split('/-->\s/', $string); // "\s" elimina los espacios

$parte_1 = $arrayString[0];
$parte_2 = $arrayString[1];

echo $parte_1; //=> PD/R
echo $parte_2; //=> PPA

See Demo

    
answered by 26.12.2016 в 19:54