mark107 Posted July 17, 2016 Share Posted July 17, 2016 I need some help with my code. I want to get the element from the tag called `<p id='categories'>`.Here is the php: <?php ini_set('max_execution_time', 300); //error_reporting(0); $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; $domdoc->recover=true; $domdoc->loadHTML($baseUrl); ?> 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`?? Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/ Share on other sites More sharing options...
ginerjm Posted July 17, 2016 Share Posted July 17, 2016 (edited) 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 July 17, 2016 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534583 Share on other sites More sharing options...
Jacques1 Posted July 17, 2016 Share Posted July 17, 2016 XPath Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534584 Share on other sites More sharing options...
mark107 Posted July 18, 2016 Author Share Posted July 18, 2016 XPath Thank you very much for this, is that the correct one that I should use? $elements = $xpath->query("*/p[@id='yourTagIdHere']"); Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534620 Share on other sites More sharing options...
mark107 Posted July 24, 2016 Author Share Posted July 24, 2016 6 days and no one is reply. does anyone know??????????????? Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534935 Share on other sites More sharing options...
ginerjm Posted July 24, 2016 Share Posted July 24, 2016 Sorry. Did you ask a question since Jacques told you what to use? PS - STOP USING THE @ CHARACTER IN YOUR CODE! Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534944 Share on other sites More sharing options...
Barand Posted July 24, 2016 Share Posted July 24, 2016 @ginerjm The "@" in that code tells xpath that "id" is an attribute. RTFM Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534953 Share on other sites More sharing options...
NotionCommotion Posted July 24, 2016 Share Posted July 24, 2016 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 */. Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534955 Share on other sites More sharing options...
Barand Posted July 24, 2016 Share Posted July 24, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534956 Share on other sites More sharing options...
NotionCommotion Posted July 24, 2016 Share Posted July 24, 2016 Thanks Barand, Where can I read up on the expression context? Specifically the first part such as "*" and "//p"? Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534957 Share on other sites More sharing options...
Barand Posted July 24, 2016 Share Posted July 24, 2016 Google took me to this one (amongst others) https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx Quote Link to comment https://forums.phpfreaks.com/topic/301495-parse-the-element-from-tag/#findComment-1534958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.