brobro Posted October 20, 2007 Share Posted October 20, 2007 Hi, I'm looking for a way to select a <div> by id from an entire xhtml page. I'm not a very experienced coder, but I'm learning... Does php have a fuction for this? it would be like $entire_page = "entire xhtml page fetched from DB which contains <div id='wanted_div'>some content I want</div>"; $my_wanted_div = .... // how do I get the content of 'iwantthisdiv' here? It would be a great help. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/74129-select-node-from-dom-by-id/ Share on other sites More sharing options...
thebadbad Posted October 21, 2007 Share Posted October 21, 2007 Okay, I actually wanted to learn this too, so i looked into it. You can do it like this: <?php $string = '<p>Some text here.</p><div id="this">Find this</div><p>Some more.</p>'; $value = preg_replace("/.*<div id=\"special\">(.*)<\/div>.*/", "$1", $string, 1); ?> All the weird stuff is regular expressions. The starting and ending forward slash in /.*<div id=\"special\">(.*)<\/div>.*/ is forward slash delimiters, which is common to use in Perl. The period matches 'any single character' (except newline) and the asterisk * matches the preceding element zero or more times. then we have <div id=\"special\"> which is the string just before the content we want. The two backslashes is used to escape the double quotation marks. Then it's (.*) You know .* matches anything but newline, and the enclosing in parentheses gives us the ability to extract whatever .* is matching right there (but remember a new line will break the code). The last part isn't too difficult. Another backslash is used to escape "/d" which has something to do with regular expressions. The $1 contains whatever is in (.*) , and that's what we are looking for. The problem here is, that you can't have newlines (line breaks) in the string you are searching. Maybe someone here can help us out.. Quote Link to comment https://forums.phpfreaks.com/topic/74129-select-node-from-dom-by-id/#findComment-374381 Share on other sites More sharing options...
thebadbad Posted October 21, 2007 Share Posted October 21, 2007 Another problem occurs if you have a nested div inside the div we want the content of, because the content then will be cut off at the nested div's closing tag. Quote Link to comment https://forums.phpfreaks.com/topic/74129-select-node-from-dom-by-id/#findComment-374383 Share on other sites More sharing options...
thebadbad Posted October 21, 2007 Share Posted October 21, 2007 Found a solution to the newline problem. Just add a 's' to the end of the weird looking part $value = preg_replace("/.*<div id=\"special\">(.*)<\/div>.*/s", "$1", $string, 1); Quote Link to comment https://forums.phpfreaks.com/topic/74129-select-node-from-dom-by-id/#findComment-374389 Share on other sites More sharing options...
brobro Posted October 21, 2007 Author Share Posted October 21, 2007 Great! However... with the unability to have nested divs... I'm really horrible with regexpressions and had hoped for the php equivalent of getElementById. This look promising however. Though i wouldn't know how to solve the nested div thing.. [it's 3 am here... going to sleep some first now] Quote Link to comment https://forums.phpfreaks.com/topic/74129-select-node-from-dom-by-id/#findComment-374393 Share on other sites More sharing options...
thebadbad Posted October 21, 2007 Share Posted October 21, 2007 Found these: http://dk2.php.net/manual/en/function.domdocument-get-element-by-id.php http://dk2.php.net/manual/en/function.dom-domdocument-getelementbyid.php But I'm not sure if these are only usable on XML documents. And the code is pretty advanced too. There must be some sort of solution to the nested div problem.. Quote Link to comment https://forums.phpfreaks.com/topic/74129-select-node-from-dom-by-id/#findComment-374605 Share on other sites More sharing options...
brobro Posted October 21, 2007 Author Share Posted October 21, 2007 Those functions sure look promising. They don't seem to be officially implemented in php yet though.. I actually solved my problem otherwise. I'm writing a ajax plugin for wordpress with the help of the jquery javascript libraries. I never wrote a plugin before though.... I want al links to posts and pages to be handled through ajax instead of normal requests, so I don't get annoying page reloads. My not so efficient and not-so-smart idea was to load the entire page requested, extract what I need and dump the rest (ie. header, footer, menu...). The solution became much easier when I found out wordpress' function to conditionally override your template file and use another one instead. So now I just get what I need into my ajaxrequested.php template file if there is an ajaxrequest... which makes much more sense altogether. Plugin engine is now almost ready, mainly need to do some boring formatting and setting up of an admin page. Nevertheless... getelementbyid in php would be a good function, for example if you want to manipulate data from other sites/api's that you get in... Quote Link to comment https://forums.phpfreaks.com/topic/74129-select-node-from-dom-by-id/#findComment-374730 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.