I have used PHP Simple HTML DOM Parser to first convert an HTML string to DOM object by str_get_html() method of simple_html_dom.php
$summary = str_get_html($html_string);
Then I extracted an <img> object from the $summary by
foreach ($summary->find('img') as $img) {
$image = $img;
break;
}
Now I needed to convert $image DOM object back to a string. I used
the Object Oriented way mentioned here:
$image_string = $image->save();
I got the error (from the Moodle debugger):
Fatal error: Call to undefined method simple_html_dom_node::save() ...
So I thought since I am working with Moodle, it may have something
to do with Moodle, so I simply did the simple (non-object oriented?)
way from the same manual:
$image_string = $image;
Then just to check/confirm that it has been converted to a string, I
did:
echo '$image TYPE: '.gettype($image);
echo '<br><br>';
echo '$image_string TYPE: '.gettype($image_string);
But this prints:
$image TYPE: object
$image_string TYPE: object
So the question is Why??? Am I doing something wrong?
You just cast it to a string in the normal way:
$image_string = (string)$image
Use outertext
$image_string = $image->outertext();
I looked in the code. function save return
$ret = $this->root->innertext();
But this is method of class simple_html_dom. After searching you receive object simple_html_dom_node. It hasn't such method and does not inherit. But has text, innertext and outertext.
$image->text();
this worked for me
Related
Everything is about PHP.
I think example will do the best.
I want the node to be <shippingCost>23</shippingCost>. But instead, xml outputs me <shippingcost>23</shippingcost>. I want to connect to an private external API, which only accepts the first form.
I was trying to use DomDocument and SimpleXMLElement classes in PHP, no of them could output me xml code without case-folding to lowercase. I was searching for some options too in both, but no of them was about lowercasing.
$input_xml = new DOMDocument("1.0","utf-8");
$super_root = addChild($input_xml,$input_xml,'orderExport','');
// ^ this gives me <orderexport></orderexport>
...
function addChild($doc,$node,$marker,$value){
$temp = $doc->createElement($marker);
$temp->appendChild($doc->createTextNode($value));
$node->appendChild($temp);
return $temp;
}
...
addChild($input_xml,$shipping_info,'shippingEmail',$mail);
...
$output = $input_xml->saveXml()
I expect to get for example
<camelCase>123</camelCase>
tag, but i get
<camelcase>123</camelcase>
I try to get data from other website like this :
https://www.wowhead.com/item=65891&xml
I also have this php code:
$xml=file_get_contents('http://www.wowhead.com/item=65891&xml');
$xml= simplexml_load_string($xml);
var_dump($xml->item->name);
but it's not work.
This will give you an object of type SimpleXMLElement:
$xml->item->name
You can use its __toString() method to get the string content:
var_dump($xml->item->name->__toString());
To get the name text contents, use __toString() method:
var_dump($xml->item->name->__toString());
//output: string 'Vial of the Sands' (length=17)
For error logging I want to save an object as string in my database. I don't want to use serialization because that triggers the __sleep()-method. So is there another way to save an object as string without using serialize()?
class Foo {
public function __toString() {
return "Hooray";
}
}
echo new Foo;
Maybe even
echo var_export(new Foo, true);
You could to this:
ob_start();
var_dump($x);
ob_get_contents();
You could try this
$string = print_r($x, true);
To save $string in the databse you could compress the string:
$string = gzcompress($string);
To uncompress and print $string use:
echo '<pre>'.gzuncompress($string).'</pre>';
an other solution might be the json_encode-function (docu).
EDIT: the nice thing about it is, that you can easily parse it with nearly any programming language if you have to automatically analyse your log files ;-)
I am uploading a file using PHP and want to return the file name and the file status to javascript. In PHP I create the json object by:
$value = array('result' => $result, 'fileName' => $_FILES['myfile']['name']);
print_r ($value);
$uploadData = json_encode($value);
This creates the json object. I then send it to a function in javascript and recieve it as a variable called fileStatus.
alert (fileStatus);
It displays
{"result":"success","fileName":"cake"}
which should be good. But when I try and do
fileStatus.result or fileStatus.fileName
I get an error saying that they are undefined. Please help I'm really stuck on this. Thanks.
The fileStatus is just a string at this point, so it does not have properties such as result and fileName. You need to parse the string into a JSON object, using a method such as Firefox's native JSON.parse or jQuery's jQuery.parseJSON.
Example:
var fileStatusObj = jQuery.parseJSON(fileStatus);
If the alert displays {"result":"success","fileName":"cake"} then you probably still have to turn the string into a JSON object. Depending on the browsers you are developing for you can use the native JSON support or the JSON.org implementation to turn your string into an object. From there on it should work as expected.
When you are setting the variable, do not put quotes around it. Just set the variable like this:
var fileStatus = <?php echo $uploadData; ?>;
or:
var fileStatus = <?=$uploadData?>;
Do not do this:
var fileStatus = '<?php echo $uploadData; ?>';
I'm trying to teach myself php... so please be kind and bear with me.
I'm trying to follow this tutorial on how to cache files... the page I want to cache is HTML only, so I've modified the php to just deal with data. I know the caching part is working, it's when I try to modify the results that I get a "Catchable fatal error: Object of class Caching could not be converted to string" in the str_replace line below.
I've tried using the __toString method here, and I've tried using serialize. Is there something I'm missing?
Edit: Oh and I've even tried casting operators.
$caching = new Caching( "my.htm", "http://www.page-I-want.com/" );
$info = new TestClass($caching);
$info = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $info );
My var_dump($caching); is as follows:
object(Caching)#1 (2) { ["filePath"]=> string(9) "cache.htm" ["apiURI"]=> string(27) "http://www.page-I-want.com/" }
Ok, I see now that the problem is with the caching.php not returning the value to the $caching string. Can anyone check out the link below and help me figure out why it's not working? Thanks!
I just posted my entire caching.php file here.
The code in on the site you link works by downloading the page from URL you give and parse it for artists and then save them to the cache file. The cache-object only contains two variables; filePath and apiURI. If you want to modify how the page is parse and converted to the cached XML-file, you should change the stripAndSaveFile function.
Here is an example of how to modify the Caching.php to do what you wanted:
function stripAndSaveFile($html) {
//mange the html code in any way you want
$modified_html = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $html );
//save the xml in the cache
file_put_contents($this->filePath, $modified_html);
}
Edit:
Other option is to extend the Caching class, in your php-code using the class you could do:
class SpecialCaching extends Caching {
var $html = "";
function stripAndSaveFile($html) {
//mange the html code in any way you want
$this->html = $html;
}
}
$caching = new SpecialCaching( "my.htm", "http://www.page-I-want.com/" );
$info = $caching->html;
$info = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $info );