0

I have a long string with a description in it. I would like to extract some information from the string. But I can not really figure out how to do it.

This is the string:

Continental CONTIPREMIUMCONTACT 2 auto/zomerband - 195/55 R15 V85. Eigenschappen EU bandenlabel: brandstofefficiëntie: F, grip op nat wegdek: C, geluid: 71dB, klasse: C1, geluidsklasse: 2 - bij www.tirendo.nl. Nu geen verzendkosten! Directe levering bij u thuis of bij een montagepunt naar keuze binnen 1-4 dagen.

I would like to retrieve the following results:

brandstofefficientie = F, grip op natwegdek = C, geluid = 71dB.

I have tried to extract it using explodes but that did not really work the way it should.

Could someone take a look at it and help me please?

2
  • Is the string always in the same format? What information is static, what changes? You can use regular expressions to get what you need, provided the string is similar each time Commented Jul 10, 2014 at 9:22
  • @Cornelius The string is different everytime. But the part that i want to extract is always the same, but then instead of a C there could be a E or D. the other text can change. Commented Jul 10, 2014 at 9:24

4 Answers 4

1

I suggest you to create an array with your info string as keys, and then perform a regexp to get the result. Something like this:

$string = "Continental CONTIPREMIUMCONTACT 2 auto/zomerband - 195/55 R15 V85. Eigenschappen EU bandenlabel: brandstofefficientie: F, grip op nat wegdek: C, geluid: 71dB, klasse: C1, geluidsklasse: 2 - bij www.tirendo.nl. Nu geen verzendkosten! Directe levering bij u thuis of bij een montagepunt naar keuze binnen 1-4 dagen.";

$myInfo = array(
    "brandstofefficientie",
    "grip op nat wegdek",
    "geluid"
);

$pattern = "/(" . implode("|", $myInfo) . ")\:\s+(.*?),/";

preg_match_all($pattern, $string, $match);

var_dump($match);

You can use some variation with the allowed flags, just to get results ordered in different ways depending on your needs, ex:

preg_match_all($pattern, $string, $match, PREG_SET_ORDER); //see PREG_SET_ORDER

source: https://www.php.net/manual/en/function.preg-match-all.php


Another alternative could be using named capturing group, as:

$pattern = "/(?<info>" . implode("|", $myInfo) . ")\:\s+(?<value>.*?),/";

preg_match_all($pattern, $string, $match, PREG_SET_ORDER);

foreach($match AS $result)
{
    echo "{$result['info']} : {$result['value']}<br>";
}
Sign up to request clarification or add additional context in comments.

Comments

0
       $text='Continental CONTIPREMIUMCONTACT 2 auto/zomerband - 195/55 R15 V85. Eigenschappen EU bandenlabel: brandstofefficiëntie: F, grip op nat wegdek: C, geluid: 71dB, klasse: C1, geluidsklasse: 2 - bij www.tirendo.nl. Nu geen verzendkosten! Directe levering bij u thuis of bij een montagepunt naar keuze binnen 1-4 dagen.=';
       $explode=explode(':',$text);
       $explode_last=explode(',',$explode[4]);
       echo $string=$explode[1]."=".$explode[2]."=".$explode[3]."=".$explode_last[0];

Comments

0

You can use following regex preg_match method to retrieve the required string values.

$text = 'Continental CONTIPREMIUMCONTACT 2 auto/zomerband - 195/55 R15 V85. Eigenschappen EU bandenlabel: brandstofefficiëntie: F, grip op nat wegdek: C, geluid: 71dB, klasse: C1, geluidsklasse: 2 - bij www.tirendo.nl. Nu geen verzendkosten! Directe levering bij u thuis of bij een montagepunt naar keuze binnen 1-4 dagen.';

preg_match('/^.*?brandstofefficiëntie: (.*?),.*?grip op nat wegdek: (.*?),*.?geluid: (.*?),/',$text,$result);

Now you can find values for brandstofefficiëntie, grip op nat wegdek and geluid in $result[1], $result[2] and $result[3] respectively.

Comments

0

If the string has exactly the same format all the time you can do something like this:

preg_match('#(.*)brandstofefficientie: (.), grip op nat wegdek: (.), geluid: (.+)dB(.*)#ui', $inputString, $matches);
echo 'brandstofefficientie: '.$matches[2].'; grip op nat wegdek: '.$matches[3].'; geluid: '.$matches[4].'dB';

My first (and not accepted) answer was

$result = preg_replace('#(.*)brandstofefficientie: (.), grip op nat wegdek: (.), geluid: (.+)dB(.*)#ui', 'brandstofefficientie = $2, grip op natwegdek = $3, geluid = $4dB.', $inputString);
echo $result;

which will return the result literally.

2 Comments

Thank you for your answer, but unfortunatly i do not get the result i want. With the code given above i just get the string i am trying to extract the data from again. But i would like to extracr the infomration from that string.
Seems I misunderstood your intention. Edited my answer, now you get the wanted values in $matches[2] to $matches[4]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.