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.com/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.com/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.com/met/351982042000000-o.jpg
Content-Description: Brick
[4] => WhateverGibberish
Content-Type: image/jpeg
Content-ID: bunchofnumbers
Object-ID: 4
Location: http://cdn.photos.sparkplatform.com/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);
?>
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.