Page 1 of 1

Parser problem in php

Posted: Mon Dec 04, 2006 11:14 am
by leewad
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi

I`m trying to Parser the following XML file:

[syntax="xml"]- <property>
  <cms_userid>45</cms_userid> 
  <currency>Euro</currency> 
  <price>199000</price> 
  <bedrooms>2</bedrooms> 
  <countryname>Spain</countryname> 
  <areaname>Las Chapas</areaname> 
  <propid>10128</propid> 
  <internalref>CPS-AP-336</internalref> 
  <propertytype>Apartment</propertytype> 
  <baths>1</baths> 
  <plotsize>97</plotsize> 
  <builtsize>70</builtsize> 
  <description>Well priced south facing apartment in Las Chapas. This property has a large lounge/dining area, 2 good size bedrooms, 1 bathroom and a terrace which looks out to the sea. This property is south facing and is part of a lovely urbanization and there are numerous swimming pools, It has underground parking. Available furnished this apartment is only 5 minutes drive from the beach.</description> 
  <photoid>61095</photoid> 
  <image_name>DSCF2862_renamed_30824.jpg</image_name> 
  <image_url>images/DSCF2862_renamed_30824.jpg</image_url> 
  <image_width>496</image_width> 
  <image_height>372</image_height> 
  <photoid>61094</photoid> 
  <image_name>DSCF2860_renamed_28355.jpg</image_name> 
  <image_url>images/DSCF2860_renamed_28355.jpg</image_url> 
  <image_width>496</image_width> 
  <image_height>372</image_height> 
  <photoid>61097</photoid> 
  <image_name>DSCF2869_renamed_2314.jpg</image_name> 
  <image_url>images/DSCF2869_renamed_2314.jpg</image_url> 
  <image_width>496</image_width> 
  <image_height>372</image_height> 
  <photoid>61099</photoid> 
  <image_name>DSCF2874_renamed_5060.jpg</image_name> 
  <image_url>images/DSCF2874_renamed_5060.jpg</image_url> 
  <image_width>496</image_width> 
  <image_height>372</image_height> 
  <photoid>61098</photoid> 
  <image_name>DSCF2876_renamed_10305.jpg</image_name> 
  <image_url>images/DSCF2876_renamed_10305.jpg</image_url> 
  <image_width>496</image_width> 
  <image_height>372</image_height> 
  <photoid>61093</photoid> 
  <image_name>DSCF2854_renamed_25832.jpg</image_name> 
  <image_url>images/DSCF2854_renamed_25832.jpg</image_url> 
  <image_width>496</image_width> 
  <image_height>372</image_height> 
  <photoid>61096</photoid> 
  <image_name>DSCF2867_renamed_19968.jpg</image_name> 
  <image_url>images/DSCF2867_renamed_19968.jpg</image_url> 
  <image_width>496</image_width> 
  <image_height>372</image_height> 
  </property>
I need the location of each image i.e[/syntax]

Code: Select all

$image1 = image_url_1;
$image2 = image_url_2;
$image3 = image_url_3;
If the tags were < image_url_1> etc I would beable to do it but having a lot of trouble trying to sort this.

Please can anyone point me in the right direction


How can I Parser the above?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Dec 04, 2006 11:27 am
by Corvin
Have a look at the XML-Functions of PHP. You can also use this class:
:arrow: http://www.phpclasses.org/browse/package/2938.html

Posted: Mon Dec 04, 2006 1:59 pm
by leewad
Thanks but i have tried looking at the php manual but cannot find a way to make an array out of the above.

Posted: Mon Dec 04, 2006 2:53 pm
by ok

Code: Select all

function BuildArrayItem($string, $name)
{
	$what2find = "<".$name.">";
	$pos = strpos($string, $what2find);
	if($pos === false)
	{
		$eof = true;
	}else{
		$eof = false;
	}

	while(!$eof)
	{
		$start_of_description = $pos + strlen($what2find);
		$temp = strpos($string, "<", $start_of_description);
		if($temp === false)
		{}else{
			$last[] = substr($string, $start_of_description, $temp-$start_of_description);
		}

		$pos = strpos($string, $what2find, $temp);
		if($pos === false)
		{
			$eof = true;
		}
	}
	return $last;
}
You call it in this way:

Code: Select all

BuildArrayItem("image_url");

Posted: Tue Dec 05, 2006 11:39 am
by leewad
Many thanks ok

But what would the names of the <image_url> be?

i need it like $image1 = <image_url[0]> etc

Posted: Tue Dec 05, 2006 11:50 am
by ok
BuildArrayItem returns array contains the content of the image_url tags (the first in 0, the second in 1 and so on...)

Posted: Tue Dec 05, 2006 12:00 pm
by leewad
Hi

but whats the string? I have tried:

Code: Select all

echo $image_url[0];
but it doesn`t contain anything

Posted: Tue Dec 05, 2006 12:05 pm
by ok
If you call it like that, you should see something:

Code: Select all

$image_url = BuildArrayItem($xml_source, "image_url");
echo $image_url[0];