I'm working on a linking system and the UI will post a hidden string below
"artwork:56,artwork:34,music:123"
This will represent linking the following media:
- Artwork ID:56
- Artwork ID: 34
- Music ID:123
What I'm trying to figure out is how to feed in the string above and spit out the id's I need.
function getArtworkIds($string){
//Code Needed
}
Results [56,34]
Any help would be awesome, Thank you
Answer:
$string = "artwork:56,artwork:34,music:123";
$a = explode(',',$string);
$ids = array();
foreach($a as $i){
if(strpos($i,"art") !== false){
$ids[] = explode(':',$i)[1];
}
}
var_dump($ids);
Output: 56,34
the
!== false
is important, as strpos
will return 0
since the string starts with art
. Without the !== false
check you'd get no results because the if
would treat the 0
as a boolean false
No comments:
Post a Comment