DOMxpath query returns nothing - php

I need to load, modify and output some 3rd party iframe on my webpage. As suggested, I had created the intermediary page that simply contains <iframe> with its src attribute.
The php code that outputs the page looks like this:
$iframe->loadHTML(file_get_contents("http://example.com/iframe_page.php")); //creating DOM object, see htm content below
$xpathObj= new DOMXPath($iframe);//creating DOMXPath object
foreach ($xpathObj->query('//div[#id="specific_id"]') as $node){ //this query returns nothing
$node->parentNode->removeChild($node);//i need to remove the div with that id, but there is nothing to remove
}
echo $iframe->saveHTML($iframe->documentElement);//the iframe output works fine
And the content of my iframe looks something like this:
<html>
<head>
</head>
<body>
<div>
<div>
<div id="specific_id">
</div>
</div>
</div>
</body>
</html>
I've even tried disabling JS to see if this div is placed there by front-end code and nope with JS disabled the structure of the document looks exactly the same.

The answer is rather simple. You CAN'T manipulate iframe element crossdomain using PHP DOMDocument.

Related

iframe adds html head and body

I have a static html document with an iframe in its html code as this
<iframe src="http://192.168.1.1/test.php"></iframe>
and test.php simply echoes a line of text:
<?php
echo '
<div class="notice">
Welcome My friend
</div>';
?>
What's strange however is that the iframe loads redundant code which I didn't add and which breaks the final DOM with a nested element:
<html>
<head>
</head>
<body>
<div class="login">...</div>
</body>
</html>
How can I avoid this extra unrequested html from being generated?
Iframe is used to embed another document within the current HTML document. So it is normal behavior of iframe tag. And you cannot handle [using CSS] or modify html code which rendered from iframe. So may be that causes your page is breaking.

Replace <body> tag with another tag

I'm trying to use the following code to replace <body> tag from page with <body id="khanqah">
echo str_replace("%body%", "khanqah", "<body id='%body%'>");
It does adds <body id="khanqah"> to the page but the actual <body> tag still presents. I mean there are two body tags now, one <body> and the other <body id="khanqah">
Also the <body id="khanqah"> tag is adding at the top of page, see this: http://i.imgur.com/6zYWTv8.jpg (screenshot of page source)
Is there any way I can work around?
It's not really replacing anything in the HTML, it's just echoing the return value of str_replace("%body%", "khanqah", "<body id='%body%'>") which happens to be the string <body id="khanqah">.
You can only replace the HTML's body element with PHP if you are outputting the HTML with PHP (changing it before outputting it). PHP works server-side, so once the HTML reaches the client it cannot modify it.
You can use JavaScript, which works client-side, to do this.
To change the id of the body dynamically using jQuery (which is the easiest way), you can do
$('body').attr('id', 'khanqah');

How to append text to external html file inside of a specific tag using PHP?

Lets say i have an external html page and I want to append text inside of a specific tag from my php admin page such as putting miscellaneous text inside of this span tag.
For example:
<html>
<body>
<span class="text"></span>
</body>
</html>
How would I do that with PHP? I'm trying to make an admin page for this website, and I need to append text inside of certain tags. I don't even know where to start with this, please point me in the right direction.
You can do this using PHP's DOMDocument, like so:
// Load the HTML document
$doc = new DOMDocument;
$doc->loadHtmlFile( 'htmlpage.html');
// Get the parent node where you want the insertion to occur
$parent = $doc->getElementsByTagName('body')->item( 0);
// Create the child element
$child = $doc->createElement( 'span');
$child->setAttribute( 'class', 'text');
// Append (insert) the child to the parent node
$parent->appendChild( $child);
// Save the resulting HTML
echo $doc->saveHTML();
So, given this HTML:
<html>
<body>
</body>
</html>
The resulting HTML would be:
<html>
<body>
<span class="text"></span>
</body>
</html>
(Neglecting the DOCTYPE declaration that DOMDocument adds if it is not present)
Depending on what exactly you need this for, you may be able to do this another way -- make that target .HTML file into a .PHP file instead, with it filling in the appropriate content itself using, let's say, a function called get_span_contents(), like so:
<html>
<body>
<span class="text">
<?PHP get_span_contents(); ?>
</span>
</body>
</html>
One downside is that this will be generated for every request of the document (potentially, depending on caching schemes), whereas if you separated it into two steps (1. Write out the HTML, 2. Serve the HTML separately) you would only ever be doing the generation once. Depending on how dynamic the content is, or how minor the computation cost, that might not be a problem.

Load a Div from another page into another pages Div

I'm trying to do this:
Load content of a div on another page
but it doesnt work for me. and I'm not sure why.
what im wanting to do is, i have a mobile site im working with, and i want to pull the story content data thats on the main sites div, and place it on the mobile sites content div that way i dont have to really edit anything. whatever gets published on the main gets reflected on the mobile.
any ideas as to the best way to accomplish this? is there a way to do like an include in php or html but that it ONLY takes the targeted divs content and not everything else?
If I'm reading you correctly, this is what you need:
$('#result').load('ajax/test.html #container');
This will load the page ajax/test.html, grab the content of the element with id "container" and load this into your current pages' element with the id "result".
More info can be found at http://api.jquery.com/load/
Edit: Working code based on your test files:
<html>
<head>
<script src="http://code.jquery.com/jquery-git.js"></script>
<script>
$(document).ready(function(){
$('#result').load('pull4m.shtml #hello');
});
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>

Loading a page using jQuery

I am using jQuery. My main page is index.php. on load of this page. How do i load another page internally. say a small block in the page ?
Like this: $('.smallBlock').load('targetUnderSameDomain.php .anotherSmallBlock');
In the first part you select the block where you want to insert the block of the targeted file.
The load function has two parts:
The first part is where you select the targeted file, the second is optional, but there you can select an element within the targeted file's html.
you can use the load command. For a simple page like this:
<html>
<head>
<title>test</title
</head>
<body>
<div id="content"> </div>
</body>
</html>
you could fill the Div with id = content like this:
$('#content').load('yourUrl.php');
EDIT: changed from get() to load()
the syntax for the get command would be:
$.get('yourUrl.php', function(data){
$('#content').html(data);
});

Categories