PHP display images if $value = $string? - php

I need to know if there's a quickest and efficient way to display one or more images if $value == $string
For example: I have an cell which only contains 3 single string: 'r o g', if user put ro it will output <img src="red.gif"> and <img src="orange.gif"> So it could be random if user insert gr then it will display <img src="green.gif"> and <img src="red.gif">
Right now I can only think something like...
<?php $red = "<img src="red.gif">";
$orange = "<img src="orange.gif">";
if( $cell1 == $red ){ echo $red;}
if( $cell1 == $red && $orange ){ echo $orange.$red;}
etc...
This method might works but has to provide too many possibility and I believe there's a shorter and efficient to do that, but I haven't got any idea because still I'm learning PHP

How about this approach?
<?php
//define all your images here
$images = [
'r' => 'red.png',
'g' => 'green.png',
'b' => 'blue.png',
'y' => 'yellow.png',
'o' => 'orange.png'
];
function output($input, $images) {
$parts = str_split($input);
foreach ($parts as $part) {
if (isset($images[$part]))
echo '<img src="' . $images[$part] . '">';
}
}
echo "rgb: \n";
output('rgb', $images);
echo "\n\nyor: \n";
output('yor', $images);
echo "\n\nxxy: \n";
output('xxy', $images);
Output:
rgb:
<img src="red.png"><img src="green.png"><img src="blue.png">
yor:
<img src="yellow.png"><img src="orange.png"><img src="red.png">
xxy:
<img src="yellow.png">

Try this approach:
for( $i = 0; $i <= strlen( $cell1 ); $i++ ) {
$char = substr( $cell1, $i, 1 );
switch ($char) {
case "r":
echo '<img src="red.gif">';
break;
case "o":
echo '<img src="orange.gif">';
break;
case "g":
echo '<img src="green.gif">';
break;
default:
break;
}
}

Here is the code that shows an example for 3 character inputs. $string can be the posted value.
$r = '<img src="red.gif">';
$y = '<img src="yellow.gif">';
$o = '<img src="orange.gif">';
$g = '<img src="green.gif">';
$string = 'ryo';
$length = strlen($string);
for ($i=0; $i<$length; $i++) {
echo ${''.$string[$i]};
}

Related

Using multiple values in switch paramater NOT case

My question is the opposite of this Using two values for one switch case statement
I'd like to use multiple values in the switch part of the statement, not the case part which I already know how to do.
<?php
$three = json_decode( $ex[ 'three-images' ] );
$i = 0;
$imgs = $trans = $class = $parallaxDir = [];
foreach ( $three as $img ) {
$imgs[ $i ] = $img->Image;
$parallax[ $i ] = $img->Transition;
$class[ $i ] = $img->Class;
//for ($ii = 0; $ii < 3; $ii++) {
switch ( $class[ $i ] ) {
case 'middle':
case 'onright':
$parallaxDir[ $i ] = '0';
break;
case 'top':
case 'onleft':
$parallaxDir[ $i ] = '1';
break;
default:
$parallaxDir[ $i ] = '9';
break;
};
//}
$i++;
}
?>
<div class="side image <?php echo $class[1]; ?>" <?php if(!empty($parallax[1])) { echo 'data-para="' . $parallax[1] . '"'; } ?> <?php if(!empty($parallaxMob)) { echo 'data-para-mobile="' . $parallaxMob . '"'; } ?> <?php if(!empty($parallaxDir[1])) { echo 'data-para-dir="' . $parallaxDir[1] . '"'; } ?>>
<img class="parallax" src="<?php echo $imgs[1]; ?>" alt="<?php echo explode('[',trim($item->title)[0]); ?> image"/>
</div>
This is the full code, seems it was working fine as I had it but for some reason when trying to implement it later down the line the array values were returning as empty even though they weren't.
So once I took the [1] off the parallaxDir if(!empty) check it started working
No, not possible.
Though you could do something like this:
foreach([$class[0], $class[1]] as $value) {
switch($value) {
// case...
}
}
But you are probably better off using if statements.

How do I filter an array after I've already created a foreach loop in PHP?

I want to mention off the bat that although I am using some Wordpress specific PHP, the answer will not have anything to do with Wordpress. I have just added it to give the full code source.
I've written a "foreach loop" to "echo" ALL of the images i've uploaded in Wordpress' media library.
However I need to be able to select all the images in a certain folder. (i.e /thisfolder).
I just don't know how to say:
"if any of the values in the $imageURL begin with:
http://localhost/testsite/wp-content/uploads/thisfolder/" then include
them, else do not."
I tried to use substr() to filter it, but I cannot seem to find a way to get it to work. Any thoughts?
CODE:
<?php
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => - 1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image ) {
$images[] = wp_get_attachment_url( $image->ID );
}
for($i = 0; $i < count($images); $i++) {
$imageURL = $images[$i];
echo $imageURL . '<br /><br />';
}
?>
RESULT:
http://localhost/testsite/wp-content/uploads/thisfolder/01.jpg
http://localhost/testsite/wp-content/uploads/thisfolder/02.jpg
http://localhost/testsite/wp-content/uploads/2016/01/03.jpg
http://localhost/testsite/wp-content/uploads/2016/01/04.jpg
I tried adding:
$URLlength = 'http://localhost/testsite/wp-content/uploads/thisfolder';
$akumalURL = substr( $URLlength, 0, strlen($URLlength)) === $URLlength;
if (in_array($akumalURL, $imageURL)) {
echo 'sucess!';
}
else {
echo 'bummer.....';
}
I would do something like
$URLlength = 'http://localhost/testsite/wp-content/uploads/thisfolder';
for($i = 0; $i < count($images); $i++) {$imageURL = $images[$i];
if(strpos($imageURL, $URLlength) === 0){
echo $imageURL . '<br /><br />';
}
}
You can do it like this,
for($i = 0; $i < count($images); $i++) {
$imageURL = $images[$i];
if ( strpos( $imageURL, 'thisfolder') !== false ) {
echo $imageURL . '<br /><br />';
}
}

PHP inside foreach issue

I have this foreach :
<?php foreach ($manufacturers as $key => $manufacturer) {
if($manufacturer->virtuemart_manufacturercategories_id == 1){
$lang = 'heb'; //Hebrew
} else {
$lang = 'eng'; //English
}
//add to letter list
$letter = mb_substr($manufacturer->mf_name, 0, 1, 'UTF-8');
${'html_letters_'.$lang}[] = $letter;
/*
echo '<pre>';
print_r($manufacturer);
echo '</pre>';*/
$link = JROUTE::_('index.php?option=com_virtuemart&view=category&virtuemart_manufacturer_id=' . $manufacturer->virtuemart_manufacturer_id);
${'html_manufacturers_'.$lang} .= '<div class="manufacturer" data-lang="'.$lang.'" data-letter="'.$letter.'"><a href="'.$link.'">';
?>
<?php
if ($manufacturer->images && ($show == 'image' or $show == 'all' )) {
${'html_manufacturers_'.$lang} .= $manufacturer->images[0]->displayMediaThumb('',false);
}
if ($show == 'text' or $show == 'all' ) {
${'html_manufacturers_'.$lang} .= '<div>'.$manufacturer->mf_name.'</div>';
}
${'html_manufacturers_'.$lang} .= '</a>
</div> <!-- /manufacturer -->';
if ($col == $manufacturers_per_row){
$col = 1;
} else {
$col++;
}
}
?>
How i check if i have more then 2 same letters and unset all others but keep one.
The output for letter is :
AABCHKIUKP
I want this will be :
ABCHKIUKP
How i do this ?
EDIT: I have updated all the foreach code. The issue is if i have more then same start letter in name EG:Aroma,Air the loop the take the first letter A and foreach him 2 times, i want to show only one if there even 10 same start letter in name .
Thanks.
When I understand you right ... just use an array
<?php
$letters = array();
foreach ($manufacturers as $key => $manufacturer) {
....
$letter = mb_substr($manufacturer->mf_name, 0, 1, 'UTF-8');
$letters[] = $letter;
}
$uni = array_unique($letters)
echo implode('',$uni);
$s = 'AABCHKIUKP';
$letters = $letters = preg_split('/(?<!^)(?!$)/u', $s); //utf8
$prev_letter = '';
$temp = '';
foreach($letters as $key => $letter) {
if (!($letter === $prev_letter)) {
$temp .= $letter;
}
$prev_letter = $letter;
}
echo $temp;
Result:
ABCHKIUKP
Info about breaking a utf8 string to an array you can find at comments here mb_split.
To strip ONLY same consecutive chars you can use this:
function stripConsecutiveChars( $string ) {
// creates an array with chars from the string
$charArray = str_split( $string );
// saves the last char thats on the new string
$lastChar = "";
// variable for the new string to return
$returnString = "";
foreach( $charArray as $char ) {
if( $char === $lastChar ) continue;
$lastChar = $char;// save current char
$returnString .= $char;// concat current char to new string
}
return $returnString;
}
$string = "AABCHKIUKP";
echo stripConsecutiveChars( $string );
In your example you could try this:
...
$letter = mb_substr( $manufacturer->mf_name, 0, 1, 'UTF-8' );
$lastLetter = end( ${'html_letters_' . $lang} );//point to last element in array
reset( ${'html_letters_' . $lang} );// reset the pointer
if( $letter === $lastLetter ) {
// i guess you just want to continue?
continue;
}
...
OUTPUT:
ABCHKIUKP

php each letter to images

I have a problem to put text into a image.
I have all letters in images on my folder named tekst.
Lets say I use $userinfo->name to get the user name and in this case the user is named zippo
Then I want the users name to return following HTML output:
<img src="tekst/z.png"><img src="tekst/i.png"><img src="tekst/p.png"><img src="tekst/p.png"><img src="tekst/o.png">
How can I do it with PHP to change each letter in the name to <img src="tekst/?.png>.
You can use this:
<?php
$name = "zippo";
for ($i = 0; $i < strlen($name); $i++) {
echo '<img src="tekst/' . $name[$i] . '.png">';
}
?>
Use str_split() function on your string and loop over result array as follows:
$letters = str_split($string);
foreach ($letters as $letter) {
echo '<img src="tekst/' . $letter . '.png" />';
}
try this one
$letters = str_split($string);
foreach ($letters as $letter) {
echo '<img src=".../tekst/' . $letter . '.png" />';
}
First you have to create a php file to convert your text to image :
<?php
/* image.php */
// Receive data
$char = $_GET['char'];
if(!empty($char)){
// This will get the first character from $char
$char = $char;
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, $char, $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
}
?>
Image String http://php.net/manual/en/function.imagestring.php
Then split its name into chars
<?php
$string = 'abcdefgh'; // For example
$chars = str_split($string);
foreach ($chars as $char) {
echo '<img src="tekst/image.php?char='.$char.'"/>';
}
?>
str_split http://php.net/manual/en/function.str-split.php
GOOD LUCK

Problems in Text to Image Replacement

I am trying to make a text replacer but since there are letters repeated i keep getting things i dont want. Does anyone know how i can do this?
Input
function image($img) {
$img = ereg_replace("a","<img src=r/a.png>", $img);
$img = ereg_replace("b","<img src=r/b.png>", $img);
$img = ereg_replace("c","<img src=r/c.png>", $img);
return $img;
}
$img = "abc";
echo image($img);
Output
<img sr<img src=r/c.png>=r/a.png><img sr<img src=r/c.png>=r/b.png><img src=r/c.png>
Output I Want
<img src=r/a.png><img src=r/b.png><img src=r/c.png>
Try this, may be insufficient but it will satisfy your requirement:
function image($img) {
$data="";
for( $i = 0; $i <= strlen($img); $i++ ) {
$char =substr( $img, $i, 1 );
switch($char)
{
case 'a':
$data .="<img src=r/a.png>";
break;
case 'b':
$data .="<img src=r/b.png>";
break;
case 'c':
$data .="<img src=r/c.png>";
break;
default:
break;
}
}
return $data;
}
$img = "abc";
echo image($img);
Problem with "c" your "a" and "b" replacing correctly but when it reach "c" there are many "c" cause "src" also added so it replaces all "c"
try with single statement
function image($img) {
$img = ereg_replace("abc","<img src=r/a.png><img src=r/b.png><img src=r/c.png>", $img);
return $img;
}
$img = "abc";
echo image($img);
Also ereg_replace() has been deprecated
Here is what I tried:-
function image($img) {
for($i=0;$i < strlen($img);$i++){
$letterarray[]=$img[$i];
}
$a=0;
foreach ( $letterarray as &$value) { // reference
$value= str_replace($value, "<img src=r/$value.png>", $value);
$a++;
$ab[] = $value;
}
return implode("",$ab);
}
$img = "abc";
echo image($img);
Here in the function image(), you don't need to specify the alphabets contained in $img

Categories