Problem with sort and then output - php

Im using the PHP below to generate some HTML output:
<?php
$url = "images.xml";
$xmlstr = file_get_contents($url);
$xml = new SimpleXMLElement($xmlstr);
$images = array();
$ids = array();
foreach ($xml->image as $image) {
$images[]['id'] = $image -> id;
$images[]['link'] = $image->href;
$images[]['src'] = $image->source;
$images[]['title'] = $image->title;
$images[]['alt'] = $image->alt;
$ids[] = $image -> id;
}
array_multisort($ids, SORT_ASC, $images);
foreach ($images as $image){
echo "<a href='".$image['link']."'><img src='".$image['src']."' alt='".$image['alt']."' title='".$image['title']."' /></a>";
}
?>
If I change the code here:
foreach ($images as $image){
echo $image['link'];
echo "Item";
}
I get the image link 3 times, which is correct because there are 3 records in the XML. But I get 12 copies of the text Item.
Why is this happening?

You're putting each attribute in a new row in the array.
Try this:
foreach ($xml->image as $image)
{
$images[] = array(
'id' => $image->id,
'link' => $image->href,
'src' => $image->source,
'title' => $image->title,
'alt' => $image->alt
);
$ids[] = $image -> id;
}

Related

How can I replace the text in a .pptx file with the phppresentation library?

I try to replace the text with another one with the phppresentation library, but it replaced my text in my file with empty $text_element_v->setText($new_text);
Here is my code:
$pptx = 'test/example.pptx';
if (!file_exists('lorem.pptx')) {
copy($pptx, 'lorem.pptx');
}
$pptReader = IOFactory::createReader('PowerPoint2007'); // PowerPoint97, PowerPoint2007
$oPHPPresentation = $pptReader->load('lorem.pptx');
$property = $oPHPPresentation->getDocumentProperties();
$slides = $oPHPPresentation->getAllSlides();
foreach ($slides as $slide_k => $slide_v) {
$shapes = $slides[$slide_k]->getShapeCollection();
foreach ($shapes as $shape_k => $shape_v) {
$shape = $shapes[$shape_k];
// echo get_class($shape).'</br>';
if($shape instanceof PhpOffice\PhpPresentation\Shape\RichText){
$paragraphs = $shapes[$shape_k]->getParagraphs();
foreach ($paragraphs as $paragraph_k => $paragraph_v) {
$text_elements = $paragraph_v->getRichTextElements();
foreach ($text_elements as $text_element_k => $text_element_v) {
$text = $text_element_v->getText();
$new_text = str_replace('Lorem', 'Esfera', $text);
$text_element_v->setText($new_text);
}
}
}
}
}
Here is the file that is empty :
What should I do please?

From array to JSON

I am making parser of articles and I need to put all parsed data in josn. I tried to put them to array and then transform it in JSON, but I have some troubles. I get JSON like this:
[{"title":"title1"}][{"title":"title2"}][{"title":"title3"}]
But I want like this:
[{"title":"title1"},{"title":"title2"},{"title":"title3"}]
How I can do this?
<?
foreach ($content_prev as $el) {
$pq = pq($el);
$date = $pq->find('time')->html();
$title = $pq->find('h3 a')->html();
$link = $pq->find('h3 a')->attr('href');
$data_link = file_get_contents($link);
$document_с = phpQuery::newDocument($data_link);
$content = $document_с->find('.td-post-content');
$arr = array (
array(
"title" => $title
),
);
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
}
Try to remove one array in $arr
Use below one.
<?
foreach ($content_prev as $el) {
$pq = pq($el);
$date = $pq->find('time')->html();
$title = $pq->find('h3 a')->html();
$link = $pq->find('h3 a')->attr('href');
$data_link = file_get_contents($link);
$document_с = phpQuery::newDocument($data_link);
$content = $document_с->find('.td-post-content');
$arr[] = array (
"title" => $title
);
}
echo json_encode($arr, JSON_UNESCAPED_UNICODE);

List Directories in Json File

I can list all the images from a folder in json using the code below:
<?php
$dir = "images/friday/";
$images = glob($dir."*.jpg");
foreach($images as $image){
$registro = array(
"FILE" => $image
);
$retorno[] = $registro;
}
$retorno = json_encode($retorno);
echo $retorno;
?>
I think it would work with directories too but nothing is displayed if I don´t use print_r($dirs).
<?php
$path = "images";
$dirs = glob($path . "/*", GLOB_ONLYDIR);
foreach($dirs as $dir){
$reg = array(
"FOLDER" => $dir
);
$return[] = $reg;
}
$return = json_encode($return);
echo $return;
?>
echo $return does not display anything. Is there a simple workaround or I have to build something like this function? I need the "FOLDER" identifier.

Nested foreach loops

I know this solution is simple, but it keeps slipping my mind. When I parse the page with this code and the $links array is printed, all of href parts are correct yet the img part only prints the last src element that is found on the page.
$doc = new DOMDocument();
$doc->loadHTML($html);
$links = array();
$images = $doc->getElementsByTagName("img");
$arr = $doc->getElementsByTagName("a");
foreach($arr as $item) {
// get links
$href = $item->getAttribute("href");
// get images.
foreach ($images as $item) {
$img = $item->getAttribute('src');
}
$links[] = array(
'href' => $href,
'img' => $img
);
}
print_r(array_values($links));
The for each statement for images should be building an array where as the final array ($links)is a multi-dimentional array($img being the nested array).
Check if this works for you:
$doc = new DOMDocument();
$doc->loadHTML($html);
$links = array();
$images = $doc->getElementsByTagName("img");
$arr = $doc->getElementsByTagName("a");
foreach($arr as $item) {
// get links
$href = $item->getAttribute("href");
// get images.
foreach ($images as $item) {
$img = $item->getAttribute('src');
// storing the image src
$links[] = array(
'img' => $img
);
}
$links[] = array(
'href' => $href
);
}
print_r(array_values($links));
You use the dublicate variable $item in internal foreach.
Try this without internal foreach
$doc = new DOMDocument();
$doc->loadHTML($html);
$links = array();
$images = $doc->getElementsByTagName("img");
$arr = $doc->getElementsByTagName("a");
foreach($arr as $key=>$item) {
// get links
$href = $item->getAttribute("href");
$img = $images[$key]->getAttribute('src');
$links[] = array(
'href' => $href,
'img' => $img
);
}unset($item);
print_r(array_values($links));

instagram tag api function

Can someone help me fix this code? result only appear 1 picture..
function get_instagram($q,$client_id) {
$api = "https://api.instagram.com/v1/tags/".$q."/media/recent?client_id=".$client_id;
$response = get_curl($api);
$images = array();
if($response){
foreach(json_decode($response)->data as $item){
$src = $item->images->standard_resolution->url;
$thumb = $item->images->thumbnail->url;
$url = $item->link;
$images[] = array(
"src" => htmlspecialchars($src),
"thumb" => htmlspecialchars($thumb),
"url" => htmlspecialchars($url)
);
return "<a href='".$url."' target='_blank'><img src='".$thumb."' border='0'/></a>";
}
}
}
What I'm trying to achieve is make all result appear in a page
In your code, you are getting only one image because, you are returning image inside loop so it dont executes all the records inside loop. Try below code
function get_instagram($q,$client_id) {
$api = "https://api.instagram.com/v1/tags/".$q."/media/recent?client_id=".$client_id;
$response = get_curl($api);
$images = array();
$returnval = '';
if($response){
foreach(json_decode($response)->data as $item){
$src = $item->images->standard_resolution->url;
$thumb = $item->images->thumbnail->url;
$url = $item->link;
$images[] = array(
"src" => htmlspecialchars($src),
"thumb" => htmlspecialchars($thumb),
"url" => htmlspecialchars($url)
);
$returnval .= "<a href='".$url."' target='_blank'><img src='".$thumb."' border='0'/></a>";
}
}
return $returnval;
}
The way you are combining the images array is not concatenating the images. To do so, use the array_merge() function. Below is the result:
function get_instagram($q,$client_id) {
$api = "https://api.instagram.com/v1/tags/".$q."/media/recent?client_id=".$client_id;
$response = get_curl($api);
$images = array();
if($response){
foreach(json_decode($response)->data as $item){
$src = $item->images->standard_resolution->url;
$thumb = $item->images->thumbnail->url;
$url = $item->link;
array_merge($images, array(
"src" => htmlspecialchars($src),
"thumb" => htmlspecialchars($thumb),
"url" => htmlspecialchars($url)
));
}
foreach ($images as $image) {
echo "<a href='".$image->$url."' target='_blank'><img src='".$image->$thumb."' border='0'/></a>";
}
}
In addition, the return statement would only execute once. In order to print "all" images, use a foreach loop for the $images array.

Categories