Getting XML element based on attribute value - php

Hey.
I have an XML file and would like to use PHP to display the medium size only. The part of the XML looks like this:
<image size="small">/small23.png</image>
<image size="medium">/medium23.png</image>
<image size="large">/large23.png</image>
I'd like to display the image on the page using <img src="" />, but I'm not sure how to put only the medium image. Within a forloop going through all the other elements, I tried this:
if($file->image->attributes()->size == "medium")
echo "<img src=$file->image />";
but nothing is drawn.
thanks

Based on your example I assume you're using SimpleXML, so try this:
$mediums = $file->xpath("image[#size='medium']");
if (count($mediums)) {
echo '<img src="' . (string) $mediums[0] . '" />';
}

$done = false;
foreach($file->image as $img) {
foreach($img->attributes() as $key => $value) {
if($key == "size" && $value == "medium") {
echo "<img src = {$file->image} />";
$done = true;
break;
}
}
if($done) { break; }
}
This might work, try it out and let me know.

Related

Convert HTML5 img to AMP amp-img with attribute conversion

I need to convert an img like this:
<img src="https://techcrunch.com/wp-content/uploads/2015/04/codecode.jpg" style="height:404px; width:602px" />
to this:
<amp-img src="https://techcrunch.com/wp-content/uploads/2015/04/codecode.jpg" height="404" width="602"></amp-img>
Keeping in mind that this code will be in a portion of code with more html tags and, important, I can't use any library or anything..
I await answers, thanks in advance!
You can Achieve your goal with following code :
function html5toampImage($string) {
preg_match('/src="(.+?)"/', $string, $src);
$srcAttribute = $src[0];
preg_match('/style="(.+?)"/', $string, $styles);
$style = $styles[1];
$allData = explode(";",$style);
foreach($allData as $data) {
if($data) {
list($key,$value) = explode(":",$data);
if(trim($key)=="height") {
$heightAttribute = trim($key).'="'.trim(str_replace("px","",$value)).'"';
}
if(trim($key)=="width") {
$widthAttribute = trim($key).'="'.trim(str_replace("px","",$value)).'"';
}
}
}
$ampImageTag = '<amp-img '.$srcAttribute.' '.$heightAttribute.' '.$widthAttribute.' layout="responsive"></amp-img>';
return $ampImageTag;
}
$html5Tag = '<img alt="aa" src="https://techcrunch.com/wp-content/uploads/2015/04/codecode.jpg" style="height:404px; width:602px; color:red" />';
echo htmlentities(html5toampImage($html5Tag));
Here is working eval url

Assign image to PHP variable

I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}
Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred
$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}
$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}
Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.
<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>
please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>

Select specific Tumblr XML values with PHP

My goal is to embed Tumblr posts into a website using their provided XML. The problem is that Tumblr saves 6 different sizes of each image you post. My code below will get the first image, but it happens to be too large. How can I select one of the smaller-sized photos out of the XML if all the photos have the same tag of <photo-url>?
→ This is the XML from my Tumblr that I'm using: Tumblr XML.
→ This is my PHP code so far:
<?php
$request_url = "http://kthornbloom.tumblr.com/api/read?type=photo";
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{'photo-caption'};
$photo = $xml->posts->post->{'photo-url'};
echo '<h1>'.$title.'</h1>';
echo '<img src="'.$photo.'"/>"';
echo "…";
echo "</br><a target=frame2 href='".$link."'>Read More</a>";
?>
The function getPhoto takes an array of $photos and a $desiredWidth. It returns the photo whose max-width is (1) closest to and (2) less than or equal to $desiredWidth. You can adapt the function to fit your needs. The important things to note are:
$xml->posts->post->{'photo-url'} is an array.
$photo['max-width'] accesses the max-width attribute on the <photo> tag.
I used echo '<pre>'; print_r($xml->posts->post); echo '</pre>'; to find out $xml->posts->post->{'photo-url'} was an array.
I found the syntax for accessing attributes (e.g., $photo['max-width']) at the documentation for SimpleXMLElement.
function getPhoto($photos, $desiredWidth) {
$currentPhoto = NULL;
$currentDelta = PHP_INT_MAX;
foreach ($photos as $photo) {
$delta = abs($desiredWidth - $photo['max-width']);
if ($photo['max-width'] <= $desiredWidth && $delta < $currentDelta) {
$currentPhoto = $photo;
$currentDelta = $delta;
}
}
return $currentPhoto;
}
$request_url = "http://kthornbloom.tumblr.com/api/read?type=photo";
$xml = simplexml_load_file($request_url);
foreach ($xml->posts->post as $post) {
echo '<h1>'.$post->{'photo-caption'}.'</h1>';
echo '<img src="'.getPhoto($post->{'photo-url'}, 450).'"/>"';
echo "...";
echo "</br><a target=frame2 href='".$post['url']."'>Read More</a>";
}
To get the photo with max-width="100":
$xml = simplexml_load_file('tumblr.xml');
echo '<h1>'.$xml->posts->post->{'photo-caption'}.'</h1>';
foreach($xml->posts->post->{'photo-url'} as $url) {
if ($url->attributes() == '100')
echo '<img src="'.$url.'" />';
}
Maybe this:
$doc = simplexml_load_file(
'http://kthornbloom.tumblr.com/api/read?type=photo'
);
foreach ($doc->posts->post as $post) {
foreach ($post->{'photo-url'} as $photo_url) {
echo $photo_url;
echo "\n";
}
}

Extract an attribute from a specific element in DOM

I want to be able to extract only the src of the second image in an html file. I am using the PHP DOM parser:
foreach($html->find('img[src]') as $element)
$src = $element->getAttribute('src');
echo $src;
However, I am getting the src of the last image in the page, instead of the one I am looking for.
Can I display only a specific src outside of the foreach loop?
Your loop is missing {}, it is equivalent to
foreach($html->find('img[src]') as $element) {
$src = $element->getAttribute('src');
}
echo $src;
so, the echo gets the $src after the last iteration of your loop, which is the last element.
Using the example from their website, I'd go with this (braces are key here):
$count = 1;
foreach($html->find('img') as $element) {
if ($count == 2) {
echo $element->src;
break;
}
$count += 1;
}

echo image url with tags in php

I have previously asked a question on how to echo the url of images from an html page. I can do this successfully but how can I narrow this down further so only images urls beginning with a certain phrase are shown, furthermore how can I add an image tag around them so the images are displayed as images and not just text?
e.g I only want to list images beginning with http://photos.website.com.
edit: I forgot to mention this is the code used to iterate through the images:
foreach($images as $image) {
echo $image->getAttribute('src') . '<br />';
}
You will have to add a condition that tests the content of $image->getAttribute('src').
To test if a string beings by another, a possibility is to use the strpos function, which returns the position of the needle in the haystack -- here, you want that position to be 0 (i.e. the first character of the string).
foreach($images as $image) {
$url = $image->getAttribute('src');
if (strpos($url, 'http://photos.website.com') === 0) {
echo $url . '<br />';
}
}
Simple:
foreach ($images as $image) {
$src = $image->getAttribute('src');
if (stripos($src, 'http://photos.website.com') === 0)
{
echo $src . '<br />';
}
}
And to add tags:
foreach ($images as $image) {
$src = $image->getAttribute('src');
if (stripos($src, 'http://photos.website.com') === 0)
{
echo sprintf('<img src="%s" alt="" />', $src) . "\n";
}
}

Categories