Mauro Lopes
Mauro Lopes

Reputation: 75

Substring right after another substring

So I have this big string, which includes in the middle the following:

"(...) Status: Ativo
Identificador: F36CE5
Meio de Pagamento: Cartão de crédito
Data de contratação: 25/03/2015
Data de expiração: 25/03/2017 (...)"

* the "(...)" is just to identify that there are more content before and after this part

My goal is to isolate only the part right after "Identificador:", i.e., I need to grab the value "F36CE5" (which is different every time, of course) and set it to a variable.

I tried the following code:

$initialString = "Status: Ativo
Identificador: F37CE5
Meio de Pagamento: Cartão de crédito
Data de contratação: 25/03/2015
Data de expiração: 25/03/2017";


$arr = explode(":", $initialString);
$important = $arr[2];
echo $important;

But with this I am getting

F37CE5
Meio de Pagamento

What should I do to get only F37CE5?

Is there a way to tell PHP: "Give the string that is right after 'Identificador:', and nothing more" ?

Upvotes: 0

Views: 77

Answers (6)

Lajos Arpad
Lajos Arpad

Reputation: 76464

A simple method is to make a substring having the correct start and then get the desired part:

$initialString = "Status: Ativo
Identificador: F37CE5
Meio de Pagamento: Cartão de crédito
Data de contratação: 25/03/2015
Data de expiração: 25/03/2017";

$substring = substr($initialString, strpos($initialString, "Identificador:") + 14);
$substring = substr($substring, 0, strpos($substring, "\n"));

Upvotes: 1

David
David

Reputation: 4361

All the other answers suggest explode and splitting lines, this is generally really slow. You should use strpos and loop through the results.

In my tests (looped 10,000 times) it took my code only 0.249 seconds on a very large test string. Under the same conditions the other code posted on this page took 0.811 seconds to do the same task.

<?php
   $pos = 0;
   while(($pos = strpos($input,"Identificador:",$pos)) !== false) {
      $end = strpos($input,"\n",$pos);
      if($end) {
         $line = substr($input,$pos,$end-$pos);
         $parts = explode(':',$line);
         echo $parts[1]."\n";
         $pos = $end;
      }else{
         $pos++;
      }
   }
?>

Upvotes: 0

Kouga
Kouga

Reputation: 26

Use regular expression. Something like:

<?php
$pattern = '/Identificador: (\w+)/';
preg_match($pattern, $initialString, $matches);
print_r($matches[1]);
?>

$matches[1] will contain the string you are looking for.

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

You can use a regex to capture the group:

preg_match("/Identificador\s*:\s*(\w+)/",$initialString,$matches);

the result is then stored in $matches[1], or the array is empty if no match is found.

Upvotes: 0

Florian
Florian

Reputation: 3171

First split the lines, then search for the one with "Identificator":

$initialString = "Status: Ativo
Identificador: F37CE5
Meio de Pagamento: Cartão de crédito
Data de contratação: 25/03/2015
Data de expiração: 25/03/2017";

$lines = explode("\n", $initialString);
foreach($lines as $line) {
  list($first, $second) = explode(":", $line);
  if (trim($first) == 'Identificator') {
    $important = $second;
    break;
  }
}
echo $important;

Upvotes: 1

Michael St Clair
Michael St Clair

Reputation: 6625

if the string is always the same length you could do this

echo substr($important, 0, 6);

Upvotes: 0

Related Questions