I
I
invisii2019-05-22 23:58:21
PHP
invisii, 2019-05-22 23:58:21

How to combine two loops in PHP?

Good day to all good people.
There is a script that looks for links in the file, walks through them and parses pictures. Divided into two cycles.
At the output, we get the downloaded pictures, the path from where it all started (the original path) and the new path to the downloaded picture.
How to combine them to get for example the view 'original path ($arr_orig[1]) -> new path ($newpath)'.
An example of the initial link that the first preg_match_all finds:

//embed.gettyimages.com/embed/537375235?et=OCOMvawjS9hbD5Z95MESBg&viewMoreLink=off&sig=4UDqZepNVDpcTJAlf-znsP3c5S-PsKb0RhWUeVWEdqM=

This is necessary in order to change the old path to the new one further in the same file. Perhaps you need to do this right away ..
So far, it turns out to collect them only separately, and then the original path gives out in the form of an array.
Below is the code itself:
$fileName = 'file.sql';
$arrs_orig = file_get_contents($fileName);
$path = dirname(__FILE__) . '/download/';
 
preg_match_all('/<iframe src=\\\"(.*?)\\\"/is', $arrs_orig, $arr_orig); 
$del038 = str_replace('#038;', '', $arr_orig[1]);
//print_r( $del038);

foreach ($del038 as $key1) {
  $result1 = file_get_contents('http:'.$key1);
  preg_match_all('/<meta property="og:image" content="(.*?)"/is', $result1, $result2); 
  $result2 = str_replace('amp;', '', $result2[1]);

    foreach ($result2 as $key2) {
          
      $parse = parse_url($key2); 
      $parts = pathinfo($parse['path']); 
      $copy = copy($key2, $path  . $parts['basename'].'.jpg');
      $newpath = '/images/' . $parts['basename'].'.jpg';
          
      print_r($newpath. PHP_EOL);		
    }
}
print_r($arr_orig[1]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maksim Fedorov, 2019-05-23
@invisii

$mapLinks = array_map(function($originalPath, $resultPath) {
    // тут делай все, что хочешь с этими двумя, например:
    return [$originalPath => $resultPath];
}, $startLinks, $resultLinks);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question