JSON output images - php

Fair warning, I'm new to JSON and trying to get the output of a JSON file to properly display in PHP.
The JSON file I have contains:
{
"C1": [
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">"
],
"C10": [
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">"
],
"C11": [
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">"
],
"C12": [
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">",
"<img src=\"available.png\">"
]
}
This file has a lot more entries of C1, C10, W1, W20, W13 etc and they are generated randomly.
I'm able to get the output of the file in php with:
$getJSON = file_get_contents('file.json');
$json = json_decode($getJSON, true);
echo '<pre>' . print_r($json, true) . '</pre>';
But I want the output to be in the html form of:
C1 <img src=\"available.png\"> <img src=\"available.png\"> <img src=\"available.png\"> <img src=\"available.png\">
For each entry
Hopefully this makes sense

Try this:
$json = json_decode($getJSON, true);
foreach($json as $key=>$val){
echo $key." ".implode($val)."<br/>";
}
You can see it here
Note: if you want a space between the <img> tags, use implode(' ', $val)

Related

String_replace replaces not all elements

I am making a Laravel Helper function, that should convert ":)" and others to normal smiles, however, some elements just do not convert! Maybe you can help me to find that, maybe stupid, mistake.
Code:
public static function convertSmiles($string)
{
$smiles = [
':)',
':angel:',
':angry:',
'8-)',
':\'(',
':ermm:',
':D',
'<3',
':(',
':O',
':P',
';)',
':alien:',
':blink:',
':blush:',
':cheerful:',
':devil:',
':dizzy:',
':getlost:',
':happy:',
':kissing:',
':ninja:',
':pinch:',
':pouty:',
':sick:',
':sideways:',
':silly:',
':sleeping:',
':unsure:',
':woot:',
':wassat:'
];
$urls = [
"<img src='".url('/jquery/sceditor/emoticons')."/smile.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/angel.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/angry.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/cool.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/cwy.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/ermm.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/grin.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/heart.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sad.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/shocked.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/tongue.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/wink.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/alien.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/blink.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/blush.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/cheerful.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/devil.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/dizzy.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/getlost.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/happy.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/kissing.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/ninja.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/pinch.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/pouty.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sick.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sideways.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/silly.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/sleeping.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/unsure.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/w00t.png'/>",
"<img src='".url('/jquery/sceditor/emoticons')."/wassat.png'/>"
];
return str_replace($smiles,$urls,$string);
}
I have problems with <3 and :'(

Return Unique Value from Chunked Array

I have a calendar script that outputs to a text file. I'm opening the text file, reading it into an array and then outputting the results. The text file contains:
7/9/2013-7/13/2013
Hot Stuff
By Robert More. Yes, folks, it's all platform shoes, leisure suits..
hotstuff.jpg
1,1,0,
*-*
7/16/2013-7/20/2013
Hot Stuff
By Robert More. Yes, folks, it's all platform shoes, leisure suits..
hotstuff.jpg
1,1,0,
*-*
My PHP code looks like this:
$content = file('DC_PictureCalendar/admin/database/cal2data.txt');
$content_chunked = array_chunk($content, 6);
if (count($content_chunked > 0))
{
echo "<table>";
for ($i=0;$i<count($content_chunked);$i++)
{
echo "<tr>";
echo "<td valign='top'>";
echo "<div style='padding-top:6px;'>";
echo "<a href='schedule.php'>";
echo "<img src='DC_PictureCalendar/admin/database/images/".$content_chunked[$i][3]."' width='80' height='80' border='2'>";
echo "</a>";
echo "</div>";
echo "</td>";
echo "<td valign='top'>";
echo "<div style='padding-left:5px;'>";
echo "<table>";
echo "<tr>";
echo "<td>";
echo "<h2>";
echo "<a href='schedule.php'>";
echo $content_chunked[$i][1];
echo "</a>";
echo "</h2>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo $content_chunked[$i][2];
echo "<a class='green' href='schedule.php'>";
echo "Read more..";
echo "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
Problem is, if there is a duplicate entry in the $content_chunked[$i][1] (which is the title in this case), I just want to display it once instead of twice. Is this possible? I thought array_unique might work but it didn't seem to help. Thanks in advance!
array_unique() RETURNS the array without duplicates, but does NOT modify the original array!
so:
$a = [1, 1, 2, 3]
array_unique($a) => [1, 2, 3]
$a => [1, 1, 2, 3]
The new array needs to be saved in a variable for you to access it later.
$a = [1, 1, 2, 3]
$b = array_unique($a)
$b => [1, 2, 3]
Although its possibly not the most graceful / efficient.. Add this in in the relevant place:
echo "<table>";
$titles = array();
for ($i=0;$i<count($content_chunked);$i++)
{
if (in_array($content_chunked[$i][1], $titles)) continue;
$titles[] = $content_chunked[$i][1]

HTML img in PHP variable

Im trying to do this
$input = "<img src="HTML/images/user.png" alt="" />";
but it does not work out,
i know im supposed to put a / before a " or something please help
Try this
$input = "<img src='HTML/images/user.png' alt='' />";
Or
$input = "<img src=\"HTML/images/user.png\" alt=\"\" />";
all you need to do is escape double quote.
just like below.
\"HTML/images/user.png\" alt=\"\"
Note that you have use " for PHP already.
You can either choose to use ' or \".
For example,
$input = "<img src='HTML/images/user.png' alt='' />";
$input = "<img src=\"HTML/images/user.png\" alt=\"\" />";
Try this
$input = "<img src='HTML/images/user.png' alt='' />";

How would I echo a slide only if the json object contains an image?

My Question:
How would I echo a slide only if the json object contains an image?
My Issue:
I'm not sure how I should treat a for loop of a json object to echo a slide only if it contains an image.
Objective:
I want to be able to echo a div only if there is an image in the json object, and then echo a link around the image so I can link to the story.
Additional:
How could I not echo a slide div inside "#slides" when there is no image?
Is there something in PHP that would let me echo the slide if it contains an image without breaking the foreach loop?
Or am I going to have to break the foreach loop,store the information for only the slides holding an image and re-loop through them? If so what would be the best way to do so?
I'm lost because if I make an if statement for $key=="img" its only going to echo the image portion, not sure how I should handle it.
Code:
NEWS.JSON
{
"1": {
"id": "1",
"img":"./images/newspost/07-05-12.jpg",
"link":"http://www.cnn.com/2012/07/05/world/europe/france-air-crash-report/index.html",
"title": "Example",
"date":"02/08/12",
"content": "Example"
},
"2": {
"id": "2",
"img":"",
"link":"http://online.wsj.com/article/SB10001424052702304141204577508500189367804.html?mod=googlenews_wsj",
"title": "Example",
"date":"09/03/10",
"content": "Example"
}
}
HOME.PHP
/* Error Report on */
error_reporting(E_ALL);
/* Open Json file */
$json = file_get_contents("./content/news.json");
/* Setup iterator to go through file */
$jsonIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($json, TRUE)),RecursiveIteratorIterator::SELF_FIRST);
/* Create SLIDES for SLIDESHOW */
echo "<div id='slides'>";
At this point I DON'T want to echo anything if no image exists.
foreach($jsonIterator as $key => $val)
{
if(is_array($val))
{
echo "<div>";
}
if($key=="link")
{
echo "<a href='$val'>";
}
if($key=="img"&&$val!="")
{
echo "<img alt='' src='$val'></img>";
}
if(!is_array($val)&&$key=="content")
{
echo "</a>";
echo "</div>";
}
}
End loop/what needs to show only if image exists.
echo "</div>";
/* End SLIDES creation */
I show you the way how I would do it:
$json = file_get_contents("./content/news.json");
$jsonArray = json_decode($json, true);
// Start slideshow...
echo "<div id='slideshow'>";
foreach ($jsonArray as $entry) {
if ($entry['img'] == '') {
continue; // Just don't do anything with this entry, go to next one
}
// News begins.
echo "<div>";
// For example:
echo "<a href='" . $entry['link'] . "'>";
// Etc.
// News ends.
echo "</div>";
}
I think it's much more simpler than your way.

PHP doesn't execute function

SOLVED: Read the comments below #Eray.
I have a PHP function to look through text and convert text emoticons to images. :), :(, :|, etc. I also have a function that looks through text and replaces BBCode with HTML. I execute these on a string from a database. Both of these use the global variable $newtext.
emoticon($row['words']);
bb($row['words']);
echo "<b>" . $row['username'] . "</b> - " . $row['time'];
echo "<p>" . $newtext . "</p>";
echo "";
The odd thing about this, is that now (I can't remember what I did) the emoticon function doesn't work, but the bb function does. By doesn't work, I mean doesn't replace anything. Text remains text. This had worked before. Also, every few times, $newtext comes before the username. Here are my functions...
function emoticon($text)
{
global $newtext;
$newtext=str_replace(":)", "<img src='emoticons/smile.gif'>", $text);
$newtext=str_replace(":(", "<img src='emoticons/sad.gif'>", $newtext);
$newtext=str_replace(":D", "<img src='emoticons/biggrin.gif'>", $newtext);
$newtext=str_replace(":p", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":P", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":|", "<img src='emoticons/neutral.gif'>", $newtext);
$newtext=str_replace("8)", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace("8D", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace(":o", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(":O", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(";)", "<img src='emoticons/wink.gif'>", $newtext);
$newtext=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $newtext);
}
function bb($text)
{
global $newtext;
$array=array(
"[b]" => "<b>",
"[/b]" => "</b>",
"[i]" => "<i>",
"[/i]" => "</i>",
"[u]" => "<u>",
"[/u]" => "</u>",
"[big]" => "<h1>",
"[/big]" => "</h1>",
);
$newtext = str_ireplace(array_keys($array), array_values($array), $text);
}
Could you explain or help me? Also, is there a better way than using global variables? I know they can be a bit "dangerous."
function emoticon($text)
{
$smiley = array(
':)',
':(',
);
$replace = array(
"<img src='emoticons/smile.gif'>",
"<img src='emoticons/sad.gif'>",
);
return str_replace($smiley, $replace, $text);
}
I think you'd better do this:
$text = $row['words'];
$text = emoticon($text);
$text = bb($text);
echo "<b>" . $row['username'] . "</b> - " . $row['time'];
echo "<p>" . $text . "</p>";
echo "";
And then edit your functions like this:
function emoticon($text)
{
// remove this line: global $newtext;
$text=str_replace(":)", "<img src='emoticons/smile.gif'>", $text);
// etc...
$text=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $text);
return $text;
}
function bb($text)
{
// remove this line: global $newtext;
$array=array(
// etc...
);
return str_ireplace(array_keys($array), array_values($array), $text);
}
First, DEFINE your PHP functions:
function emoticon ( $string )
{
$emoticons = array( ':)' , ';)' );
$icons = ('happy.gif','wink.gif');
return str_replace( $emoticons, $icons , $string );
}
function bb( $string )
{
//BOLD [b]text[/b]
$string = preg_replace('/(\[b\]([\w\d\s\.]+)\[\/b\])/i','<b>$2</b>',$string);
//ITALIC [i]text[/i]
$string = preg_replace('/(\[i\]([\w\d\s\.]+)\[\/i\])/i','<em>$2</em>',$string);
//UNDERLINE [u]text[/u]
$string = preg_replace('/(\[u\]([\w\d\s\.]+)\[\/u\])/i','<u>$2</u>',$string);
return $string;
}
Second, Call you PHP defined functions:
echo bb( "[b]Bold[/b]" ); //Return <b>Bold</b>
Bold
echo bb( "[i]Italic[/i]" ); //Return <em>Italic</em>
Bold
echo bb( "[i]My [b]Text[/b][/i]" ); //Return <em>My <b>Text</b></em>
My Text
$var = $_POST['foo'];
echo bb( $var );
Third, Test your code:
emoticon($row['words']);
bb($row['words']);
echo "<b>" . $row['username'] . "</b> - " . $row['time'];
echo "<p>" . $newtext . "</p>";
echo "";
didnt test it but this should do:
notice that instead of using global $newtext i'm creating a local variable and return it to the outer scope via return.
function emoticon($text) {
$newtext=str_replace(":)", "<img src='emoticons/smile.gif'>", $text);
$newtext=str_replace(":(", "<img src='emoticons/sad.gif'>", $newtext);
$newtext=str_replace(":D", "<img src='emoticons/biggrin.gif'>", $newtext);
$newtext=str_replace(":p", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":P", "<img src='emoticons/tongue.gif'>", $newtext);
$newtext=str_replace(":|", "<img src='emoticons/neutral.gif'>", $newtext);
$newtext=str_replace("8)", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace("8D", "<img src='emoticons/cool.gif'>", $newtext);
$newtext=str_replace(":o", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(":O", "<img src='emoticons/surprised.gif'>", $newtext);
$newtext=str_replace(";)", "<img src='emoticons/wink.gif'>", $newtext);
$newtext=str_replace("^<**>^", "<img src='emoticons/crab.gif'>", $newtext);
return $newtext;
}
function bb($text) {
$array=array(
"[b]" => "<b>",
"[/b]" => "</b>",
"[i]" => "<i>",
"[/i]" => "</i>",
"[u]" => "<u>",
"[/u]" => "</u>",
"[big]" => "<h1>",
"[/big]" => "</h1>",
);
return str_ireplace(array_keys($array), array_values($array), $text);
}
$row['words'] = emoticon( $row['words'] );
$row['words'] = bb( $row['words'] );

Categories