codecreative Posted September 27, 2013 Share Posted September 27, 2013 Hi could someone please explain to me what is going on in this segment of code $entry['images'] = array(); foreach ($property as $property_field => $property_field_value){ if(!empty($property_field_value) && preg_match('/MEDIA_IMAGE_[0-9]{2}/', $property_field)){ $image_path = $config['image_destination'] . DS . $property_field_value; if(file_exists($image_path)){ $entry['images'][] = array('path' => $image_path, 'url' => $this->blmConfig['files_url'] . '/images/' . $property_field_value); } } } Link to comment https://forums.phpfreaks.com/topic/282470-hi-could-someone-please-explain-to-me-what-is-going-on-in-this-segment-of-code/ Share on other sites More sharing options...
requinix Posted September 27, 2013 Share Posted September 27, 2013 set the "images" thing in $entry to an empty array for each thing in $property, using $property_field as the key and $property_field_value as the value { if the value is not empty and it starts with "MEDIA_IMAGE_" and two digits { set $image_path as the "image destination" from $config, plus DS, plus the value if that file exists { add to the "images" thing that particular array of values } } } Link to comment https://forums.phpfreaks.com/topic/282470-hi-could-someone-please-explain-to-me-what-is-going-on-in-this-segment-of-code/#findComment-1451364 Share on other sites More sharing options...
gizmola Posted September 27, 2013 Share Posted September 27, 2013 It's going through an array named $property, looking for entries that match '/MEDIA_IMAGE_XX' and if found in that array element, creating a string which is the configured path to a directory where I assume images are to be found. If files actually exist in that directory, it then adds each entry to an array ($entry['images']) which will contain one or more arrays with the path to the file and a url. I'm assuming this is for some sort of gallery system or media management system. What specifically don't you understand about the code? Link to comment https://forums.phpfreaks.com/topic/282470-hi-could-someone-please-explain-to-me-what-is-going-on-in-this-segment-of-code/#findComment-1451365 Share on other sites More sharing options...
codecreative Posted September 27, 2013 Author Share Posted September 27, 2013 Hey great forum I'm happy to have found it and impressed with the quick response. This was my first post on php freaks. So this code is a small segment from a parser I had developed. When someone uploads an xml file and images to a web server, the xml file is parsed and they get inserted as entries into a database. And is output onto a website as the website reads from the database. I'm just looking through the code and trying to self educate myself and came across this. I got the gist of what is going on but wasn't too sure about the regular expression being used, I'm not sure what MEDIA_IMAGE refers to as none of the image files are called that. An example of the image filename is, 1111_103_IMG_01.jpg I'm enjoying learning php and I have a basic understanding, as I am an able java oo developer. So I'm aware of data structures, loops, oo principles etc and I understand php on a basic level but this is threw me a bit. Link to comment https://forums.phpfreaks.com/topic/282470-hi-could-someone-please-explain-to-me-what-is-going-on-in-this-segment-of-code/#findComment-1451373 Share on other sites More sharing options...
vinny42 Posted September 27, 2013 Share Posted September 27, 2013 and it starts with "MEDIA_IMAGE_" and two digits The regexp checks if the string contains that, anywhere inside the string. So "fooMEDIA_IMAGE_12345" also matches (a very common and bugsensitive mistake). I'm not sure what MEDIA_IMAGE refers to Literally that text: "MEDIA_IMAGE". Link to comment https://forums.phpfreaks.com/topic/282470-hi-could-someone-please-explain-to-me-what-is-going-on-in-this-segment-of-code/#findComment-1451381 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.