Parser problem in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Parser problem in php

Post 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]
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Post 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
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Post 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.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post 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");
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Post by leewad »

Many thanks ok

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

i need it like $image1 = <image_url[0]> etc
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

BuildArrayItem returns array contains the content of the image_url tags (the first in 0, the second in 1 and so on...)
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Post by leewad »

Hi

but whats the string? I have tried:

Code: Select all

echo $image_url[0];
but it doesn`t contain anything
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post 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];
Post Reply