Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:38:57+00:00 2026-05-31T04:38:57+00:00

I’ve got an XML snippet and it has a section where an URL is

  • 0

I’ve got an XML snippet and it has a section where an URL is segmented as shown below. I can successfully parse it using SimpleXML and XPath. My XPath queries return me an array with the results, and then I can echo them. No probs! =)

However I want to do it a step further trying to reduce code lines and improve my code. First take a look at my code:

<?php

$xml_g='
<files>
    <file>
        <filename>itzafile</filename>
        <ext>.tar.gz</ext>
        <url protocol="http://">itzanexample.net/folder/subfolder/</url>
    </file>
    <file>
        <filename>itzavideo</filename>
        <ext>.mp4</ext>
        <url protocol="ftp://">itzanotherurl.com/videos/</url>
    </file>
</files>
';


function URLparts($xml) {
$xmlData= simplexml_load_string("$xml");

$protocol = $xmlData->xpath('//file/url/@protocol');
$url = $xmlData->xpath('//file/url');
$filename = $xmlData->xpath('//file/filename');
$ext = $xmlData->xpath('//file/ext');

echo 'The following it\'s echoed calling 4 different arrays:'."\n\t".$protocol[0], $url[0], $filename[0], $ext[0]."\n\t".$protocol[1], $url[1], $filename[1], $ext[1]."\n\n"; //prints the entire url! Right!


//These variables are arrays, so theoretically it must be possible to create an array of arrays here:
$completeURL = array($protocol,$url,$filename,$ext);
//I've also tried this but it's just the same problem:
//$completeURL = array(array($protocol),array($url),array($filename),array($ext));

echo 'The following should be echoed with a two-dimensional array, but something fails:'."\n\t".$completeURL[0][0][0][0]."\n\n"; //Just prints "http://" WRONG! u.u
/*
 * $completeURL[1][0][0][0] prints the domain+subfolder
 * $completeURL[0][1][0][0] prints ftp://
 * $completeURL[0][0][1][0] prints nothing...
 */

}

URLparts($xml_g);
?>

As you can see, I want to avoid joining the URL as: echo $protocol[0], $url[0], $filename[0], $ext[0] and I want to do it simpler with fewer variables as in completeURL[0][0][0][0] (first file node with all it’s corresponding parts), completeURL[1][1][1][1] (second file node with all the url parts) etc…

But obviously I’m doing something wrong. Where is the error? It should be related to the multidimensional array I’m trying to create.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:38:58+00:00Added an answer on May 31, 2026 at 4:38 am

    If done this way:

    $completeURL = $xmlData->xpath('//file/url/@protocol | //file/url | //file/filename');
    echo $completeURL[2].$completeURL[1].$completeURL[0]."\n";
    echo $completeURL[5].$completeURL[4].$completeURL[3]."\n";
    

    The echo command prints this:

    http://itzanexample.net/folder/subfolder/itzafile.tar.gz
    ftp://itzanotherurl.com/videos/itzavideo.mp4
    

    The use of the union operator | gets the intended result using PHP5 + SimpleXML + XPath !!
    HOWEVER I don’t understand WHY the result must be echoed backwards (2, 1, 0; 5, 4, 3) instead of 0, 1, 2, 3, etc.

    Also I don’t get WHY the following expression gives the same result echoing the same way:

    $completeURL = $xmlData->xpath('//file/filename | //file/url | //file/url/@protocol');
    

    This also solves the question but it remains unknown for me why these 2 expressions return the same. So the order doesn’t matters? Why it’s needed to echo backwards??

    So this is an answer indeed but need to be explained. Somebody knows why?? THANKS! =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.