phpQuery: Replace all occurrences of text with another - php

I am trying to parse a website homepage to convert it into xml file to be used as an api in my app.
So far I have successfully done so. However, the parsed text contains the & (ampersand) character which causes the XML parser to fail.
I am looking for a solution that doesn't use the CDATA or doesn't output CDATA in the XML file.
I want to replace & with and at every occurrence. What phpQuery method should I use?
This causes error in browser because the text() method returns a text with
& character in it.
require('phpQuery/phpQuery.php');
$all=phpQuery::newDocumentFileHTML('BPUT.htm', $charset = 'utf-8');
$links = $all['a.myblue'];
echo '<notice>';
foreach ($links as $link) {
echo '<text>';
echo pq($link)->text();
echo '</text>';
echo '<url>';
echo pq($link)->attr('href');
echo '</url>';
}
echo '</notice>';
?>
I do not want to use CDATA, as the CDATA tag is visible in the generated XML :
<?php
header('Content-type: text/xml');
require('phpQuery/phpQuery.php');
$all=phpQuery::newDocumentFileHTML('BPUT.htm', $charset = 'utf-8');
$links = $all['a.myblue'];
echo '<notice>';
foreach ($links as $link) {
echo '<text>';
echo "<![CDATA[";
echo pq($link)->text();
echo "]]>";
echo '</text>';
echo '<url>';
echo pq($link)->attr('href');
echo '</url>';
}
echo '</notice>';
?>
bumping for answers.

Related

extract XML tag content with PHP

I have a PHP script that extracts data from an XML and so far it only looks for tag attributes. How can I also extract the tag content?
XML
<test name="Example 1">
<status status="FAIL" starttime="20200501 09:36:52.452" endtime="20200501 09:37:07.159"
critical="yes">Setup failed:
Variable '${EMAIL_INPUT}' not found.</status>
</test>
PHP
foreach ($result->test as $result) {
echo $result['name'], PHP_EOL;
$endtime = $result->status;
echo $endtime['starttime'], PHP_EOL;
echo $endtime['endtime'], PHP_EOL;
echo $endtime['status'], PHP_EOL;
}
What I need is the text in-between the tags:
"Setup failed:Variable '${EMAIL_INPUT}' not found."
Thanks
To get the contents of a node you can just cast node to string:
// I changed to `as $test` 'cause `as $result`
// overwrites initial `$result` variable
foreach ($result->test as $test) {
$endtime = $test->status;
$text = (string) $endtime;
// Also `echo` will cast `$endtime` to string implicitly
echo $text;
}

PHP XML Foreach shows 1 review

Iam trying to loop a XML file with Foreach but doesn't work. There are like 30 reviews in the XML File but only shows one. It shows the first person in the list but then on the bottom.
Iam trying to get better at PHP so dont know allot about it for now.
This is the code that i use.
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-
wezemer%20' or die ('Niet verbonden');
$xml = simplexml_load_file($url);
foreach ($xml as $rijschool){
echo 'Voornaam: '.$rijschool->beoordeling->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->beoordeling->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->beoordeling->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beoordeling->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->beoordeling->aanbeveling.'<br>';
echo 'Service: '.$rijschool->beoordeling->service.'<br>';
echo 'Deskundigheid: '.$rijschool->beoordeling->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->beoordeling-
>prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->beoordeling->gemiddelde.'<br>'.'<br>';
}
?>
Edit: here is the XML file Link https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20
And here is what iam getting what current code shows
I think this is what you're trying to do:
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20';
$xml = simplexml_load_file($url);
foreach ($xml->beoordelingen->beoordeling as $rijschool){
echo 'Voornaam: '.$rijschool->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->aanbeveling.'<br>';
echo 'Service: '.$rijschool->service.'<br>';
echo 'Deskundigheid: '.$rijschool->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->gemiddelde.'<br>'.'<br>';
}
?>
The problem you are having is that your foreach is iterating over the topmost node, but you want to iterate over a node lower down in the tree.

Echo XML Elements with file_get_contents

i'm successfully managing to pull in the information in this feed:
http://api.zoopla.co.uk/api/v1/zed_index?area=yo1&output_type=outcode&api_key=XXXXMYAPIKEYGOESHEREXXXXX
I'm doing this with the following code:
<p><?php $postcode = get_the_title(); $str = urlencode($postcode); $url = "http://api.zoopla.co.uk/api/v1/zed_index?area=$str&output_type=outcode&api_key=5dj2d5x8kd2z2vnk9g52gpap"; $string = file_get_contents($url); echo $string;?></p>
However, this just echos the following output:
DE45 http://www.zoopla.co.uk/home-values/de45 53.258037 53.138911 -1.580861 -1.79776 England Derbyshire 53.198474 -1.6893105 DE45 368929 375424 362103 372926 333441 329349 322644 368056
How could i adapt my existing code to successfully echo individual elements from the feed, for example just the following fields wrapped in tags:
zed_index
zed_index_1year
zed_index_2year
Thanks for your help!
You could use simplexml_load_file() to get an array which will contains every of your XML tags :
<p>
<?php
$XML_url = 'http://api.zoopla.co.uk/api/v1/zed_index?area=yo1&output_type=outcode&api_key=XXXXMYAPIKEYGOESHEREXXXXX';
$XML_parsed = simple_xml_load($XML_url);
// print for debug
echo '<pre>';
print_r( $XML_parsed );
echo '</pre>';
// Access one of the tag
$tagName = $XML_parsed['tagName'];
// Access a nested tag
$nestedTag = $XML_parsed['first_tag']['second_tag'];
?>
</p>

Read feed with PHP correct encoding?

I'm using the following code to read a RSS feed. The problem is that I get wrong encoding. The code is in a file with UTF-8 encoding. Is there anything else I have to do to get it right?
$feed_url = "http://lujanenlinea.com.ar/noticias/feed";
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<div class='rss-container'>";
echo "<ul class='rss-content'>";
foreach($x->channel->item as $entry) {
echo "<li><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></li>";
}
echo "</ul>";
Maybe use utf8_encode() or utf8_decode()

PHP - 'wrap' <a> tag around any <img> tag within a string

I need to take any img tag within a string, and add an a tag around it.
E.g.
$content= "Click for more info <img src="\http://www.domain.com/1.jpg\"" />";
Would need to be replaced with
"Click for more info <a href=\"http://www.domain.com/1.jpg\"<img src="\http://www.domain.com/1.jpg\"" /></a>";
My current script is:
$content = $row_rsGetStudy['content'];
$doc = new DOMDocument();
$doc->loadHTML($content);
$imageTags = $doc->getElementsByTagName('img');
foreach($imageTags as $tag) {
$content = preg_replace("/<img[^>]+\>/i", "<img src=\"$tag\" />", $content);
}
echo $content
This gives me the following error:
Catchable fatal error: Object of class DOMElement could not be converted to string
Any ideas on where I'm going wrong?
With DOM methods, something like this (untested, debug yourself ;P )
foreach($imageTags as $tag) {
$a = $tag->ownerDocument->createElement('a');
$added_a = $tag->parentNode->insertBefore($a,$tag);
$added_a->setAttribute('href',$tag->getAttribute('src'));
$added_a->appendChild($tag);
}
$content is an object which can not be converted to string.
to test it, use var_dump($content);
you can't directly echo it.
use properties and methods which offer by DOM, you can get from here: DOM Elements
getElementsByTagName returns DOMNodeList object containing all the matched elements. So $tag is DOMNodelist::item here and hence can't be used directly in string operations. You need to get nodeValue. Change the foreach code as follows:
foreach($imageTags as $tag) {
$content = preg_replace("/<img[^>]+\>/i", "<img src=\"$tag->nodeValue\" />", $content);
}
I think here the DOMDocument is not loading HTML from the string. Some strange issue. I prefer you to use DOM parser such as SimpleHTML
You can use it like :
$content= 'Click for more info <img src="http://www.domain.com/1.jpg" />';
require_once('simple_html_dom.php');
$post_dom = str_get_html($content);
$img_tags = $post_dom->find('img');
$images = array();
foreach($img_tags as $image) {
$source = $image->attr['src'];
$content = preg_replace("/<img[^>]+\>/i", "<img src=\"$source\" />", $content);
}
echo $content;
Hope this helps :)

Categories