1

I'd like to get some values out of an array and print them out on the page.

For [1], these things should be extracted: USD 7.0269 6.4119 0.14231 0.15596

The array looks like this:

print_r($arr);
[1] => USD United States of America 7.0269  6.4119  Dollars 0.14231 0.15596  � Copyright 2003-2011. Powered by CurrencyXchanger 3.580 
[2] => EUR  Euro Member Countries 9.0373    8.3253  Euro    0.1107  0.1201   � Copyright 2003-2011. Powered by CurrencyXchanger 3.580

What is the best solution to accomplish this?

5 Answers 5

1

I'd use preg_match_all() after I trim off the area of interest:

foreach ($arr as $line) {
    // currency is in the first four characters (apparently)
    $currency = substr($line, 0, 4);

    // we use everything left of 'Copyright'
    $rest = strstr($line, 'Copyright', true);

    // match each occurrence of nn.nnnn
    if (preg_match_all('/\d+\.\d+/', $rest, $matches)) {
        // $matches[0] contains all the amounts
        echo $currency, ' ', join(' ', $matches[0]), PHP_EOL;
    }
}

For PHP < 5.2 you need this line to calculate $rest:

$rest = substr($line, 0, strpos($line, 'Copyright'));

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

No need for the capturing group. Otherwise, +1!
Looks good. I get this error though "Wrong parameter count for strstr()"
Works like a charm! I had to change to $currency = substr($line, 0, 4); as well. Thanks a lot!
1

Here is a regex solution:

foreach($arr as $key => $item)
{
    preg_match('/^([A-Z]){3}[\sA-Za-z]+(\d+\.\d+)\s+(\d+\.\d+)\s+[A-Za-z]+\s+(\d+\.\d+)\s+(\d+\.\d+)/', $item, $matches);
    $result[$key] = array_shift($matches);
}

The regex corresponds to your pattern and captures everything you want inside consecutive elements of $matches. Since $matches[0] represents the full match, we remove the first element and assign it to your result array.

Comments

0

Try

 foreach($arr as $v) {
  $items = explode(' ', $v);
  $new_arr[] = $items[0]; //Getting the currency type
  foreach($items as $k => $m) {
    if(is_numeric($m) && floor($m) != $m && $k != (count($items) - 1))
      $new_arr[] = $m;
  }
 }

 //displaying the $new_arr
 foreach($new_arr as $n) {      
  if(is_numeric($n) === FALSE)
    echo "\n";     
    echo $n . ' ';
 }

See it in action here

2 Comments

That would also get the last numbers. (Although, I don't know whether that is an actual issue)
Edited my answer, to not get the last numbers.
0

Done quickly:

$result = array_map(
    function ($string) {
        preg_match_all('/(\d+\.\d+)\s/', $string, $matches);
        return substr($string, 0, 3) . ' ' . implode(' ', $matches[1]);
    },
    $arr
);

Result:

Array
(
    [0] => USD 7.0269 6.4119 0.14231 0.15596
    [1] => EUR 9.0373 8.3253 0.1107 0.1201
)

Comments

0

With Regular Expressions you can get it.

foreach($arr as $key => $value) {
    preg_match_all('/(\d+\.\d+)/', $value, $matches);
    $result[substr($value, 0, 3)] = array_shift($matches);
}

You get an Array like this

var_dump($result);
array (
    'USD' => array( 7.0269, 6.4119, 0.14231,  0.15596 )
    'EUR' => array( 9.0373, 8.3253, 0.1107,  0.1201 )
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.