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)
Related
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
I'm trying to display search-results with the sign & in them. But when I render from php to json & converts to &.
Is there anyway I can prevent that, or before I print the names in the search-bar convert it back to &?
Thanks in advance!
EDIT:
HTML JS:
{
name: 'industries',
prefetch: 'industries_json.json',
header: '<h1><strong>Industries</strong></h1>',
template: '<p>{{value}}</p>',
engine: Hogan
},
industries_json.json (Created with json_encode)
[{"id":42535,"value":"AUTOMOBILES & COMPONENTS","type":"industries"}]
php-script which ouputs json:
public function renderJSON($data){
header('Content-type: application/json');
echo json_encode($data);
}
Use html_entity_decode function like...
Code Before:
public function renderJSON($data){
header('Content-type: application/json');
echo json_encode($data);
}
output: Gives html code in string like ex: appthemes & Vantage search Suggest
Code After:
public function renderJSON($data){
header('Content-type: application/json');
$data = json_encode($data);
echo html_entity_decode( $data );
}
output: Gives html code in string like ex: appthemes & Vantage search Suggest
The issue you're facing is HTML encoding. What you want is to decode the text before sending it to the client. It is important that you only decode the text properties of the JSON object (rather than the entire JSON object itself).
Here is a reference on HTML decoding in PHP: http://php.net/manual/en/function.html-entity-decode.php
I would do it in javascript. Once you have got the JSON, just use replace function of javascript on the JSON like this:
first convert it to string by this:
var jsonString=JSON.stringify(jsonData);
then replace & like this.
jsonString=jsonString("&","&");
then again, convert it to JSON obj;
jsonObj=JSON.parse(jsonString);
now, your JSON, will have & instead of &.
What's next ?
Do whatever you need to do with the jsonObj
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Help me parse this file with PHP
I need to extract some text from a text file.suppose there is a text file in http://site.com/a.txt
And the contents of that file is like this:
var $name= 'name1';
var $age= 'age2';
var $phone= 'phonenumber';
var $a= 'asd';
var $district= 'district23';
How can I get the values of this text file (name1,age2,phonenumber,asd,district) in separate echo.
Use the file function to read the file into an array. Then loop through the array and each line in the file will be another element in the array. So make sure your file has line-breasks between the data.
Of course the best would be to have ready PHP code in a .php file which would then be included with the include function.
Organize the content of your text file like this : name1,age2,phonenumber,asd,district
And do this :
// Get the content of your file
$content = file_get_contents('a.text');
// Set your values with this
list($name, $age, $phone, $a, $district) = explode(',', $content);
// Then feel free to echo wathever you want
echo 'Name ' . $name;
Use an array and encode it: http://uk.php.net/manual/en/function.json-encode.php
I recommend using a class to encapsulate your data...
So imagine having a file called "person.php" that looks like this...
class Person
{
public $Name;
public $Age;
public $Phone;
public $A;
public $District;
}
You can then use the person class as a container.
include_once('person.php');
$person = new Person();
$person->Name = 'John Doe';
$person->Age = 52;
$person->Phone= '+441234 567 890';
$person->A = 'asd';
$person->District = 'District23';
Please note that "Age" is volatile (i.e. if the object lives for too long, the age will wrong!) You could avoid this by storing date of birth and then having a getAge() function on the Person object that gives you the correct age at any point in time.
The Person class is a plain PHP object, but you could add functions that add behaviour that relates to the concept of a Person, so the getAge() function would live on the Person class.
Finally, you could then store the object wherever you like using PHP's serialize and unserialize functions. The stored string that represents your object would look like this:
O:6:"Person":5:{
s:4:"Name";s:8:"John Doe";
s:3:"Age";i:52;
s:5:"Phone";s:15:"+441234 567 890";
s:1:"A";s:3:"asd";
s:8:"District";s:10:"District23";
}
And here is how you serialize the $person to look like this:
$serializedPerson = serialize($person);
echo $serializedPerson;
And converting from a string back to a Person is easy too:
$serializedPerson = 'O:6:"Person":5:{s:4:"Name";s:8:"John Doe";s:3:"Age";i:52;s:5:"Phone";s:15:"+441234 567 890";s:1:"A";s:3:"asd";s:8:"District";s:10:"District23";}';
$newPerson = unserialize($serializedPerson);
echo $newPerson->Name;
Summary
So if you stored you data in this serialized format, it is really easy to convert it directly into a PHP object that you can use without manually parsing the strings. You could store the string in a text file if you wanted - or a data store.
Rather than giving you the solution code I'm going to break this down into steps for you.
0) write the file in a machine readable format, e.g. name,age,phonenumber
1) Read from the file line by line
2) Break each line up according to the separator you used e.g. ","
3) Read in values into variables
4) Echo out
If you're stuck on something more specific, let us know.
I'm trying to extract the href attribute of link as a string.
Read More
I'm using the following to read the extract the attribute:
$link = simplexml_load_string($ad['meta_value']);
$order['logo'] = $logo['href']->asXML();
Instead of getting http://example.com I'm getting href="http://example.com". Beside using str_replace() is there a way to extract the attribute as a string?
Treat #attributes as object and convert to string:
$link = simplexml_load_string($ad['meta_value']);
echo (string) $link->attributes()->href;
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; ?>';