Jump to content

Parse the element from tag

Recommended Posts

I need some help with my code. I want to get the element from the tag called `<p id='categories'>`.

Here is the php:

    ini_set('max_execution_time', 300);
    $errmsg_arr = array();
    $errflag = false;
    function getState($string)
      $ex = explode(" ",$string."  ");
      return $ex[1];
    $xml = "";
    $xml .= '<?xml version="1.0" encoding="UTF-8" ?>';
    $xml .= '
    <tv generator-info-name="www.mysite.com/xmltv">';
    $baseUrl = file_get_contents('www.myscript.com/get-listing.php');
    $domdoc = new DOMDocument();
    $domdoc->strictErrorChecking = false;

Here is the html source:

    <p id='categories'>Sports</p>

I have no idea how to parse the element from the `<p id='categories'` tag.

Can you please show me an example how I can parse the element from the `<p id='categories' tag` to make it show as `Sports`??

Link to post
Share on other sites

PHP gets its input from form elements via the Get and POST arrays. You know that, don't you?


Sorry - now I see that you are parsing this by reading some other script.


So you are beginning to use the dom functions. Have you read up on examples of how to parse that document yet?

Edited by ginerjm
Link to post
Share on other sites


Thank you very much for this, is that the correct one that I should use?

$elements = $xpath->query("*/p[@id='yourTagIdHere']");



I too need to better understand the query syntax. I believe  [@id=yourTagIdHere] is fine.  I've only currently used the class attribute, and used something like '//p[@class=yourClassHere]'Maybe you don't need the p since the id should uniquely identify the element type?  I believe // will search from the current node and children, but not sure about */.

Link to post
Share on other sites

Find any elements with id attribute = 'category'

$elements = $xpath->query("*[@id='category']");

Find "p" elements anywhere in the document with id attribute = 'category'

$elements = $xpath->query("//p[@id='category']");

As stated, an id should be unique, so either method should do it

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

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

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.