DOMDocument::saveHTML($domnode) in PHP 5.2? - php

I have several functions in a class that return saveHTML(). After I echo more than one function in the class saveHTML(), it repeats some of the HTML. I initially solved this by doing saveHTML($node) but that doesn't seem to be an option now.
I didn't know saveHTML($domnode) was only available in PHP 5.3.6 and I have no control over the server I uploaded the files to so now I have to make it compatible with PHP 5.2.
For simplicity's sake it and only to show my problem it looks similar to this:
<?php
class HTML
{
private $dom;
function __construct($dom)
{
$this->dom = $dom;
}
public function create_paragraph()
{
$p = $this->dom->createElement('p','Text 1.');
$this->dom->appendChild($p);
return $this->dom->saveHTML();
}
public function create_paragraph2()
{
$p = $this->dom->createElement('p','Text 2.');
$this->dom->appendChild($p);
return $this->dom->saveHTML();
}
}
$dom = new DOMDocument;
$html = new HTML($dom);
?>
<html>
<body>
<?php
echo $html->create_paragraph();
echo $html->create_paragraph2();
?>
</body>
</html>
Outputs:
<html>
<body>
<p>Text 1.</p>
<p>Text 1.</p><p>Text 2.</p>
</body>
I have an idea why it's happening but I have no idea how to not make it repeat without saveHTML($domnode). How can I make it work properly with PHP 5.2?
Here's an example of what I want to be able to do:
http://codepad.viper-7.com/o61DdJ

What I do, is just save the node as XML. There are a few differences in the syntax, but it's good enough for most uses:
return $dom->saveXml($node);

You have return $this->dom->saveHTML(); twice in your class ( as far as I know you don't have to return it inside the class anywhere unless it is a private function.
If you take return $this->dom->saveHTML(); out of createparagraph() it will echo without returning. It's a DOM thing as far as I know but am new to this like you.

Related

Return HTML code inside PHP function return

I am very new to PHP and I was handed over a project that has some existing PHP code.
I was asked to add hyperlink to the email address, so I want to know how to add HTML code in the function return.
public function mail()
{
return (new Mail)
->subject('Welcome')
->message('Send questions to', xxx#email.com);
}
Let's assume that everything in the Mail class works. Both of the subject and message methods return the strings inside the brackets.
Can anyone tell me if this code works? I can't run the program for some reason
Usually we write like this
public function returnHtml {
echo "//and here we write html code"
//for example
echo "<h1> hi<h1>"
}
also you can try something like this
function TestBlockHTML ($replStr) {
return <<<HTML
<html>
<body><h1>{$replStr}</h1>
</body>
</html>
HTML;
}

Integrate HTML-Code from other classes to own WP plugin

I want to write a new Plugin in wordpress. My classes:
the plugin PHP-file:
<?php
/*
plugin-header (working)
*/
// Exit if accessed directly
defined('ABSPATH' || exit());
// Include classes
include('foo.php');
include('boo.php');
//add Init Hook
add_action('admin_menu','bohoo_admin');
function bohoo_admin() {
add_options_page('bohoo', 'someTitle', 'manage_options', __FILE__, 'createView');
}
function createView() {
$foo = new foo();
$boo = new boo();
return $foo->createFooDiv() . $boo->createBooDiv();
}
?>
My foo.php:
<?php
class foo {
public function __construct() {
}
public function createFooDiv() {
return '<div><h2>Hi</h2></div>';
}
}
?>
My boo.php:
<?php
class boo {
public function __construct() {
}
public function createBooDiv() {
return '<div> test </div>';
}
}
?>
Now what I basically tried is: The HTML-code should be in two different files and these should be concatenated and displayed (of course).
The way I understood integrating plugins in WP:
With the add_options_page-method you specify where your plugin is shown and what code is displayed. For the code you use the last argument (in this case the createView-method. This works so far if my createView() in my plugin PHP-file looks like this:
function createView() {
//include HTML-Code directly
?>
<h1>Hello World</h1>
<?php
}
?>
What happens when I include the boo.php and foo.php files instead of including the HTML-Code directly is nothing (So nothing is displayed and there is on error aswell). I am not sure what I am doing wrong, I also tried playing around with the HTML-Code in the return-statements of BOO and FOO, but that did not help either. What am I doing wrong? Or is it simply not possible to do it that way?
You need to print/echo your html content. Change createView to this:
function createView() {
$foo = new foo();
$boo = new boo();
echo $foo->createFooDiv() . $boo->createBooDiv();
}
That's how it's done in the add_options docs page.
And basically when you use php closing and opening tags:
?>
<h1>Some html</h1>
<?php
It's equivalent to printing the content in between:
echo '<h1>Some html</h1>';

Using DOMDocument Validate() on XML Causes Infinite Load

I have an extremely simple implementation that pulls in a test bit of XML and attempts to validate it using DOMDocument. In testing, it's able to get through the LoadHTML() call fine, but as soon as I try and run validate(), the browser hangs forever and doesn't load. Here's the code:
$content = '<?xml version="1.0" encoding="utf-8"?><mainElement></mainElement>';
$dom = new DOMDocument;
$dom->LoadHTML($content);
if (!$dom->validate()) {
echo 'fail';
} else {
echo 'success!';
}
It seems that if you want to validate content loaded with loadHTML, you need DOCTYPE declaration (without it, you get an infinitive loop). For example, following code works and prints fail
$content = "
<!DOCTYPE html>
<html>
<body>
Content of the document......
</body>
</html>
";
$dom = new DOMDocument();
$dom->loadHTML($content);
if (!$dom->validate()) {
echo 'fail';
} else {
echo 'success!';
}
For XML it's more tolerant (it works even you didn't declare dtd but it returns false). In your case, you might use loadXML method and your code will print fail.
Tested with php 7.0.13.

Can't retrieve parameters

I'm playing with OOP in PHP.
I have this code in my index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Numbers Class</title>
</head>
<body>
<?php
require_once("numbers.php");
$numbers->start("1","2","3");
echo $numbers->list_numbers();
?>
</body>
</html>
And this in my numbers.php
<?php
// Class creation
class numbers
{
private $n1, $n2, $n3;
// Method creation
public function start ($n1,$n2,$n3)
{
$this->number1=$n1;
$this->number2=$n2;
$this->number3=$n3;
}
public function list_numbers()
{
return $this->number1;
return $this->number2;
return $this->number3;
}
}
// Object instance
$numbers=new numbers();
?>
Now, if what I have read so far about oop in PHP, my output should be
1
2
3
But I'm only getting
1
Why???
What I'm doing wrong???
I'm creating a new object called numbers, it has 3 attributes, I created 2 methods, one for storing the numbers and another to call them back.
I load the class and send the numbers, but somehow I'm failing when calling them back. I lost the second and third number, and I just don't understand why...
With return you're getting out of the function, you can use it only once per function so.
I guess what you want is the echo function
public function list_numbers()
{
echo $this->number1;
echo $this->number2;
echo $this->number3;
}
In your list_numbers() method, you are returning the first value. From http://php.net/manual/en/function.return.php
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return will also end the execution of an eval() statement or script file.
If you replaced return with echo in your list_numbers() method, you would get the output you are expecting.
Also, you didn't "lose" the numbers. You can verify this by doing a var_dump($this).
Antoine's and Ben's explanations are good, but the suggested repair might also be
public function list_numbers()
{
return array($this->number1, $this->number2, $this->number3);
}
In this case, you get array from the function by
$nums = $numbers->list_numbers();
You can now print this array or do anything else you might want with it, the same way you would do it with any other array.
Change to this...
public function list_numbers()
{
return array($this->number1, $this->number2,return $this->number3);
}
Then in your php file....
<?php
require_once("numbers.php");
$numbers->start("1","2","3");
$nums = $numbers->list_numbers();
//use it in a loop or just print_r it or whatever....
foreach($nums as $val){
echo "Number is : ".$val;
}

Can't load the web page content using php5 domdocument

<?php
class parsedictionary {
public function _process() {
$webpage="http://www.oppapers.com/essays/Computerized-World/160871?read_essay";
$doc=new DOMDocument();
$doc->loadHTML($webpage);
echo $doc;
}
}
$obj=new parsedictionary();
$obj->_process();
?>
I can't get the content of that page.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<body>
<p>http://www.oppapers.com/essays/Computerized-World/160871?read_essay</p>
</body>
</html>
But i need to get the content of that page.
The DOMDocument class is obviously not a string; you can iterate it, perform operations on it, but it can't just be echoed. Check the documentation to see what you can do with it: http://www.php.net/domdocument
To get the page contents, you can either use file_get_contents or do echo $doc->saveHTML()
Edit: Didn't realize you had another problem in your code; you can just use this instead:
public function _process() {
return file_get_contents('http://www.oppapers.com/essays/Computerized-World/160871?read_essay');
}
<?php
$doc->saveHTML();
?>
Works like a charm.
Well the error is pretty clear in this case. The _process() method cannot convert from one data type to another, it is expecting String and your feeding it a DomDocument. Perhaps you should try to extract all the text out of the DomDocument first as a string and then send that to the _process() method.

Categories