admin管理员组

文章数量:1356413

I run (Real Estate RETS) queries that will only return objects. The contents of these objects appear to be strings that contain different types of information about sets of images (ie. several different pictures from the same house). All I want is the link to each picture ("Location:" in the sample below), put into an array that I can use elsewhere in the script.

I have figured out how to accomplish this using a nested set of explode() commands, but I was wondering if there is a more direct, efficient way to do this: something like ltrim(delete everything to the end of the word "Location: "), and then rtrim(delete everything after ".jpg") leaving me with each value in an array like $picturelinks.

Here is a sample of the data I'd like to do this with.

Array (
    [1] => WhateverGibberish
    Content-Type: image/jpeg
    Content-ID: bunchofnumbers
    Object-ID: 1
    Location: .jpg
    Content-Description: Welcome
    Preferred: 1
    
    [2] => WhateverGibberish
    Content-Type: image/jpeg
    Content-ID: bunchofnumbers
    Object-ID: 2
    Location: .jpg
    Content-Description: Welcome home
    
    [3] => WhateverGibberish
    Content-Type: image/jpeg
    Content-ID: bunchofnumbers
    Object-ID: 3
    Location: .jpg
    Content-Description: Brick

    [4] => WhateverGibberish
    Content-Type: image/jpeg
    Content-ID: bunchofnumbers
    Object-ID: 4
    Location: .jpg
    Content-Description: Brix
)

Here is the php code that does accomplish the task, but I wonder if there is a method that would run faster:

<?php
$photos = $rets->GetObject("Property", "HiRes", $sysid, "*", 1); //1 gets links
foreach($photos as $photo) { //array includes everything for all photos      
    unset ($photo['Success']); //not needed
    unset ($photo['Content-Type']); //not needed
    unset ($photo['Length']); //not needed
        
    $pile=explode('--',$photo['Data']);
    unset ($pile[0]);
    foreach ($pile as $blah) {
        $subpile=explode('Location: ',$blah);
        $picstuff[]=$subpile[1];
    }
    foreach($picstuff as $piclilnk) {
        $breakme=explode('Content-Description: ',$piclilnk);
        $linksonly[]=$breakme[0];
        $otherstuff[]=$breakme[1];
    }
    //print_r($linksonly);
    //print_r($otherstuff);
?>

I run (Real Estate RETS) queries that will only return objects. The contents of these objects appear to be strings that contain different types of information about sets of images (ie. several different pictures from the same house). All I want is the link to each picture ("Location:" in the sample below), put into an array that I can use elsewhere in the script.

I have figured out how to accomplish this using a nested set of explode() commands, but I was wondering if there is a more direct, efficient way to do this: something like ltrim(delete everything to the end of the word "Location: "), and then rtrim(delete everything after ".jpg") leaving me with each value in an array like $picturelinks.

Here is a sample of the data I'd like to do this with.

Array (
    [1] => WhateverGibberish
    Content-Type: image/jpeg
    Content-ID: bunchofnumbers
    Object-ID: 1
    Location: http://cdn.photos.sparkplatform/met/347305719000000-o.jpg
    Content-Description: Welcome
    Preferred: 1
    
    [2] => WhateverGibberish
    Content-Type: image/jpeg
    Content-ID: bunchofnumbers
    Object-ID: 2
    Location: http://cdn.photos.sparkplatform/met/350148712000000-o.jpg
    Content-Description: Welcome home
    
    [3] => WhateverGibberish
    Content-Type: image/jpeg
    Content-ID: bunchofnumbers
    Object-ID: 3
    Location: http://cdn.photos.sparkplatform/met/351982042000000-o.jpg
    Content-Description: Brick

    [4] => WhateverGibberish
    Content-Type: image/jpeg
    Content-ID: bunchofnumbers
    Object-ID: 4
    Location: http://cdn.photos.sparkplatform/met/353549178000000-o.jpg
    Content-Description: Brix
)

Here is the php code that does accomplish the task, but I wonder if there is a method that would run faster:

<?php
$photos = $rets->GetObject("Property", "HiRes", $sysid, "*", 1); //1 gets links
foreach($photos as $photo) { //array includes everything for all photos      
    unset ($photo['Success']); //not needed
    unset ($photo['Content-Type']); //not needed
    unset ($photo['Length']); //not needed
        
    $pile=explode('--',$photo['Data']);
    unset ($pile[0]);
    foreach ($pile as $blah) {
        $subpile=explode('Location: ',$blah);
        $picstuff[]=$subpile[1];
    }
    foreach($picstuff as $piclilnk) {
        $breakme=explode('Content-Description: ',$piclilnk);
        $linksonly[]=$breakme[0];
        $otherstuff[]=$breakme[1];
    }
    //print_r($linksonly);
    //print_r($otherstuff);
?>
Share Improve this question edited Mar 27 at 23:39 Barmar 784k57 gold badges548 silver badges660 bronze badges asked Mar 27 at 22:15 phc_joephc_joe 1033 silver badges14 bronze badges 6
  • 1 Please 1) show us the actual code you wrote, rather than just describing it. Then it'll be a lot easier to tell you if there's another way to do it. 2) provide the sample data in code format (you can use var_export() to get it out of your application if necessary) so it can easily be re-used in answers and experiments without tedious re-keying being needed by those who might want to help you, and 3) define what exactly you mean by "efficient". What's your metric? Less code? Runs faster? Fewer function calls? Is your current method causing a specific problem? Please edit your post. Thanks. – ADyson Commented Mar 27 at 22:22
  • Ok. Fair critique. Edits/code added. – phc_joe Commented Mar 27 at 22:31
  • Thanks. Data format could still be made easier for us to re-use. Also, how fast does this run currently, for given sizes of array? Is the speed giving you problems? How much of an improvement would be useful? – ADyson Commented Mar 27 at 23:24
  • WOW! preg_match('/^\slocation:\s*(.)/im', $p, $match)) worked great! I added microtime() to completely stripped down versions of the query script (one with my explodes() and the other with preg_match() and tested each five times. Nested explodes() version: 2.2705860137939 2.2768650054932 0.53050899505615 0.9162609577179 0.95514106750488 Pregmatch version: 0.58952713012695 0.59466981887817 0.51377582550049 0.49790787696838 0.52830195426941 – phc_joe Commented Mar 28 at 15:07
  • 1 Why all the down votes for this question? I did a careful search prior to posting and did not find anything that I understood clearly enough to apply. Many thanks to those who did bother to help! – phc_joe Commented Mar 28 at 15:17
 |  Show 1 more comment

2 Answers 2

Reset to default 2

Use a regular expression to find the lines that begin with Location: and extract the rest of the line using a capture group.

foreach ($pile as $p) {
    if (preg_match('/^\s*location:\s*(.*)/im', $p, $match)) {
        $linksonly[] = $match[1];
    }
}

If you need other values you can extract them similarly.

Let's suppose $pile is the array you mentioned (after "Here is a sample of the data I'd like to do this with.").

You can list all the strings next to "Location:" in the same line using a regex like this:

$picturelinks = array_map(
    function($string) {
    preg_match('/^Location:\s*([^\r\n]+)/m', $string, $matches);
    return $matches[1] ?? null;
    },
    $pile
);

But I suppose you can parse $photo['Data'] once:

preg_match_all('/^Location:\s*([^\r\n]+)/m', $photo['Data'], $matches);
$picturelinks = $matches[1] ?? [];

I tested it using: https://onlinephp.io/

本文标签: phpIs there a more efficient way to extract links from an array of stringsStack Overflow