Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/74129-select-node-from-dom-by-id/
Share on other sites

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..

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]

 

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..

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....  ;D

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.