Replace <body> tag with another tag - php

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');

Related

DOMxpath query returns nothing

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.

How to insert css code in php?

I know how to insert html code in php using print <<<HERE HERE; construction. It means I can insert a link to css file. But is it possible to insert css code itself?
There are a couple of ways you can achieve this.
Lets say you have an anchor tag My Href and you want to add CSS to it based on certain instances.
You can easily echo that css into the anchor element like so:
<a <?php echo 'style="float: left;"'; ?> href="..">My Href</a>
The output would be:
<a style="float: left" href="...">My Href</a>
You can do this with almost any HTML element.
Now let's say you have a predefined CSS class you would like to echo into an anchor tag element.
Let's say you have this CSS class in your stylesheet:
.floatLeft {
float: left;
}
You can simply echo that class into the anchor element like so:
My Href
Which the output of that would be:
My Href
Yes. It is possible.
There's actually two different ways.
Method 1: Inject PHP into your actual CSS. This is done by making your file .php, using regular HTML and CSS, and then doing as such:
.someclass {
/* Some code */
<?php // Whatever you wanna inject
?>
I have performed this many times, and no one knows. It may not be the most efficent way, but it's also sometimes better than doing...
Method 2: Escape your quotes and lines.
Use:
echo("/* Your css code here */ \ (to escape the line return
/* Whenever there is a quote, make sure to do the following: \" ");
If you don't escape the quote, PHP will think you're ending the string. That's bad, because the rest of your text gets ignored.
ADDITIONAL NOTE: You don't have to use print for echoing HTML. Echo works too.
Yes, you can easily do that. You can either do it in the CSS file like 'ilarsona' said, or you can do it in the HTML file itself.
For example:
<!DOCTYPE html>
<html>
<head>
<title>The Title</title>
<style>
p {<?php *Insert dynamic styles here* ?>};
</style>
</head>
<body>
<div style=<?php *Insert styles here* ?>> </div>
</body>
</html>

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.

PHP or HTML first or does it matter?

I have a possible stupid question, but I'll ask it anyway.
Does it matter what goes first, PHP or HTML code?
For example: Does PHP go before HTML, after HTML or does it matter at all?
<?php
echo "This is text";
?>
<html>
<head>
</head>
<body>
<center>
<font size="2">This is text</font>
</center>
</body>
</html>
Or:
<html>
<head>
</head>
<body>
<center>
<font size="2">This is text</font>
</center>
</body>
</html>
<?php
echo "This is text";
?>
Or:
<html>
<head>
</head>
<body>
<?php
echo "This is text";
?>
</body>
</html>
The third one is the correct way (assuming you want the text to echo out in the body).
PHP can jump in and out of HTML as you have shown above.
<html>
<head>
</head>
<body>
<center>
<font size="2"><?php echo "This is text"; ?></font>
</center>
</body>
</html>
Personally I put the PHP as much as possible at the top of the page or even better outside the html page completely by using the html pages as purely views in the MVC pattern.
HTML doesn't go anywhere, but PHP script goes to server executes and response is returned to client side. Now that response is displayed/handled along with HTML code. HTML is only for browser where PHP script is used invoke service or do operations on database.
So, first PHP(Server) and then HTML(Client).
Add your php code before the html code.
This allows you to change the out type, set requied variables, add http response headers if you require, etc.
You can have a lot of php embeded tags in between the html.
The html in your question would be invalid, if you echoed output before or after .
Make sure your out is valid html.
Don't be bad to the browser just cause they will try to work with whatever you give them.
Onlything you have to maintain valid html structure. so you canot put anything outside the html tag. so third option is the most valid thing. but if you use any of others, it will print anything you want.
Not being a php person, will try to ans this in general sense. HTML is for browsers and php is serverside. When your pages reaches to the browser, there is only HTML, while, if I am not wrong as php should behave similarly yo jsp, at serverside html is seen as simple strings that need to be printed out at stream. So ideally, this should not matter what come first.
From good practice perspective, as this is php code(in my case jsp) whose output will be html, I try to give more feel of java to my code file.

How to display an HTML email for preview on a webpage?

I am trying to display a "preview" of an HTML email. I have the HTML in my database and now I need to render it in an iframe, or popup window or something. I am trying to inject the html into a div tag on the page, but it won't display anything. Here is the problem I am running into (I have nested HTML tags):
<html>
<body>
<h1>My page</h1>
<div id="email-body">
<html>
<body>
<p>email</p>
</body>
</html>
</div>
</body>
</html>
You can write HTML to a popup window.
var preview = window.open("", /* options */);
preview.document.write(html);
preview.document.close();
But like me, many dislike popup windows. Another consideration is to just display only the <body> contents. Even better, most mail clients just supports it. With a content type of text/html, you can send a HTML mail as if it is going to be part of a HTML <body>.
<p>email</p>
This way you can for preview just inject it in some div in the main page the usual way.
If you like to style elements, but dislike inline styles, you can also add <style> element. Most of those mail clients also just supports it.
<style>p { font-family: arial, sans-serif; }</style>
<p>email</p>
From HTML purist's view this is indeed syntactically invalid. But it works (also in webbrowsers!) and eases the stuff a lot up.
Assuming that the markup looks exactly like you showed in your post, you could just strip the tags out like this (run this on the email field after you get it from your db):
$email = preg_replace('/\<\/?(html|body)\>/', '', $email);
This will leave you just the body content of the email. This will work as long as the email doesn't have anything in between <html> and <body>, such as a <head> section.
That won't work because you can't have two HTML structures in a document.
The only way to do this that I can see is using an iframe.

Categories