Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_replace, regex getting Text Parts

I have the following problem:

I have a Text with the e.g. the following Format:

min: 34.0 max: 79.0383 lifetime: 17% code:iweo7373333

It's not a fixed text Type, means min can also be -7.94884444 or so. How can i extract the parts in e.g. an array like

$result['min'] = 34.0;
$result['max'] = 79.0383
and so on...

I did it at the moment with replacing spaces, then replace "min:" with nothing, "max:", "lifetime:", ... with "," and then an explode... The main Problem is that sometimes other variables are between min, max, .... so the positions do not hold the correct values.

Also - i think - it's not a really good coding style or? Is this possible with regex or preg_replace?

Thanks, Sascha

like image 464
codeworxx Avatar asked Dec 01 '25 12:12

codeworxx


2 Answers

There's nothing "bad" about using preg_replace or regex. It's certainly not ideal to be parsing this unformatted string, though. If you can modify the source string, try JSON or XML for more reliable results. At the very least, even a url format would work better (e.g. min=123&max=456&limit=789).

Now on to the main question:

// test data
$result = array('min' => false, 'max' => false, 'lifetime' => false);

// match any occurence of min/max/lifetime followed by : followed by text (anything not a space)
if( preg_match_all('/\b(min|max|lifetime): +([^ ]+)/', $string, $matches, PREG_SET_ORDER) ) {
   foreach($matches as $m) {
      $result[$m[1]] = $m[2]; // put each match into $result
   }
}
var_dump($result); // see what we got back
like image 147
Kato Avatar answered Dec 04 '25 07:12

Kato


Also - i think - it's not a really good coding style or?

There is no need to be authoritative about it. It depends on your purposes. I would personally opt for JSON in this case. XML can be an overkill most of the times. The only advantage I see in keeping that format you proposed is that it has no need for complex syntax using {}()[];, (and it seems you don't need nesting).

This regex will match all the parameter:value combinations from your string, being very tolerant with use of whitespace on values:

(?<=^| )[A-Za-z-_]{0,}:[.,\$\-\+\s%\w]{0,}(?<=\s|\Z|^)

So in PHP:

$string = "simple:I like to exchange data a-css-like-parameter: 34px CamelCasedParameter: -79.0383 underlined_parameter: 17%";

preg_match_all('/(?<=^| )[A-Za-z-_]{0,}:[.,\$\-\+\s%\w]{0,}(?<=\s|\Z|^)/', $string, $matches);

$parameters = array();
foreach($matches[0] as $parameter){
    $exploded = explode(':', $parameter);
    $parameters[$exploded[0]] = trim($exploded[1]);
}

print_r($parameters);

Output:

> Array
> (
>    [simple] => I like to exchange data
>    [a-css-like-parameter] => 34px
>    [CamelCasedParameter] => -79.0383
>    [underlined_parameter] => 17%
> )
like image 41
marcio Avatar answered Dec 04 '25 08:12

marcio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!