0

I have this 2 strings:

String 1:

AV. LOS TALLERES NRO. 4898 URB. EL NARANJAL LIMA - LIMA - INDEPENDENCIA

String 2:

LA JOYA MZA. I LOTE. 13 -B (PLAZA PRINCIPAL DE LA JOYA) AREQUIPA - AREQUIPA - LA JOYA

I want to output only from 1st string:

LIMA - LIMA - INDEPENDENCIA

and from 2nd string:

AREQUIPA - AREQUIPA - LA JOYA

I'm receiving those strings and I have to show only that part. There're always 3 parts and separated with - but the text changes, for example: what I receive could be one of:

AREQUIPA - AREQUIPA - AREQUIPA

LIMA - LIMA - ATE

PROV. CONST. DEL CALLAO - PROV. CONST. DEL CALLAO - CALLAO

AREQUIPA - CAMANA - CAMANA

How can I split them? I tried counting from the end to the last space after the -, but I feel there is a better way.

Community
  • 1
  • 1
Oscar
  • 23
  • 4
  • 1
    Is there only ever one word before the first `-`? Or can you have two words like in `LA JOYA`? Because that could make it quite complex. – Obsidian Age Aug 19 '19 at 22:29
  • It could be two words. – Oscar Aug 19 '19 at 22:34
  • Have you tried anything? Maybe a [string function](https://www.php.net/manual/en/ref.strings.php) like [`strtok`](https://www.php.net/manual/en/function.strtok.php)? – ficuscr Aug 19 '19 at 22:34
  • 1
    Possible duplicate of [PHP: Split string](https://stackoverflow.com/questions/5159086/php-split-string) – ficuscr Aug 19 '19 at 22:36

2 Answers2

0

I think this might help you:

  1. We need to separate the strings with ' - ':
$string = "AV. LOS TALLERES NRO. 4898 URB. EL NARANJAL LIMA - LIMA - INDEPENDENCIA";
$farr = explode(' - ',$string);

=> [
     "AV. LOS TALLERES NRO. 4898 URB. EL NARANJAL LIMA",
     "LIMA",
     "INDEPENDENCIA",
   ]
  1. Since you need the last part of the first portion on the string:
$sarr = explode(' ',$farr[0]);

=> [
     "AV.",
     "LOS",
     "TALLERES",
     "NRO.",
     "4898",
     "URB.",
     "EL",
     "NARANJAL",
     "LIMA",
   ]

3 print with:

echo end($sarr).' - '.$farr[1].' - '.$farr[2]

In the first, I'm using end to print only the last part of the second array. If you want to print more parts you'll need to change it

Jorge Lima
  • 101
  • 3
  • 2
    And what happend if i have "PROV. CONST. DEL CALLAO - PROV. CONST. DEL CALLAO - PROV. CONST. DEL CALLAO"? – Oscar Aug 19 '19 at 23:27
  • @Oscar following my example it will print: **CALLAO - PROV. CONST. DEL CALLAO - PROV. CONST. DEL CALLAO**. My code will split the sting in three parts by ' - ' then it will split the first position of the array by the ' ' and finally it will print the last position of the second split, the second position of the first split and the last position of the first split. – Jorge Lima Aug 20 '19 at 00:39
-1

*Current string -

$String1="AV. LOS TALLERES NRO. 4898 URB. EL NARANJAL LIMA - LIMA - INDEPENDENCIA";

$String2="LA JOYA MZA. I LOTE. 13 -B (PLAZA PRINCIPAL DE LA JOYA) AREQUIPA - AREQUIPA - LA JOYA";

*We need to split string to array

$arr1 = explode('-',$String1);

$arr2 = explode(') ',$String2);

*Create new string

$String1 = substr($arr1[0],-5).' - '.$arr1[1].' - '.$arr1[2];

$String2 = $arr2[1];

*Print final make new string

echo $String1; echo '<br>';

echo $String2;