Jump to content

Hi could someone please explain to me what is going on in this segment of code


Go to solution Solved by gizmola,

Recommended Posts

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);
					}
				}
			}

 

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
}
}
}
Edited by requinix
  • Solution

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?

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.

 


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".

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.