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
|
Show 1 more comment
2 Answers
Reset to default 2Use 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
版权声明:本文标题:php - Is there a more efficient way to extract links from an array of strings? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744065576a2584928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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