I got this in the video script that I use for my website (Videos Page Layout):
<div class="video-views pull-left">
{$videos[i].viewnumber|kilo} {if $videos[i].viewnumber == '1'}{t c='global.view'}{else}{t c='global.views'}{/if}
</div>
Related videos section uses Ajax for videos page layout generated with the "show more" button.
My problem is: I don't know how to convert the "kilo" function in Ajax {$videos[i].viewnumber|kilo}. I attempted a few things but with no result.
$code[] = '<div class="video-views pull-left">';
$views = ($video['viewnumber'] == '1') ? $lang['global.view'] : $lang['global.views'];
$code[] = $video['viewnumber']. ' '.$views;
$code[] = '</div>';
Kilo is not a modifier that is included in the Smarty distribution, but if you have access to the code on your site, you can extract the code from the plugin file, it is most likely in smarty/libs/plugins/modifier.kilo.php.
It looks like you're working in PHP to construct a response to an AJAX request, so you can just pull that code out and re-use it.
If you do not have access to the modifier file, you can just recreate the formatting on your own. Judging by the context, it's something simple like:
<?php
/**
* Format integers into human readable strings indicating number of thousands
* Example: 1200 -> 1.2K
* #param int $value
* #return string
*/
function kilo(int $value): string
{
// If the value is less than 1000, just return it
if($value < 1000)
{
return $value;
}
/*
* If the value is evenly divisible by 1000, we want to show a whole number,
* otherwise format it as a single precision float. Add "K" string literal
* to indicate thousands
*/
$formatString = ($value % 1000) ? '%.1fK':'%dK';
// Divide value by 1000
$value /= 1000;
// Return the formatted string
return sprintf($formatString, $value);
}
// Define some test data and echo the formatted values
$testViewCounts = [1, 123, 1230, 12300, 123000];
foreach($testViewCounts as $views)
{
// If only one view, do not pluralize
$label = ($views == 1) ? 'view':'views';
echo kilo($views).' '.$label.PHP_EOL;
}
Related
Is there a way I can replace the 2nd character in a cell with an asterisk(*)?
Something like this:
var name = publicWinners.getRange(i, 1);
name.setValue( → 2nd character = "*" ← );
I want to create a list of winners in a contest that can be posted publicly with the winners' personal information partially hidden.
Any help would be greatly appreciated. Thank you!
Use String.replace :
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
const str = "John Doe";
const output = str.replace(/(.{1})./,"$1*");
console.info(output);
<!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
. - Any character
(.{1}) - Any character repeated 1 time. {1} may be removed, but used to quantify previous characters, if you want to replace the third or fourth character. Capture group$1
. - Final character to replace with *
You can use the split() and join() methods to manipulate the string.
/**
* Replace a character of a string with a new character at the specified position.
* #param {string} word - The word to modify.
* #param {number} index - The character index to modify. Starts from zero.
* #param {string} newChar - The new character that will overwrite the existing character.
* #returns {string}
*/
function replaceCharAt(word, index, newChar) {
return word.split('').map(function(char, charIndex) {
if (charIndex == index) {
return newChar;
}
return char;
}).join('');
}
A simple test you can run:
var originals = [
'Sangmyeong',
'Jaedong',
'Sangjin',
'Gueho'
];
originals.forEach(original => console.log(replaceCharAt(original, 1, '*'))); // [S*ngmyeong, J*edong, S*ngjin, G*eho]
function replaceCharAt(word, index, newChar) {
return word.split('').map(function(char, charIndex) {
if (charIndex == index) {
return newChar;
}
return char;
}).join('');
}
Please take a look at this function :
function my_func($index_bis) {
if (!is_int($index_bis) || $index_bis > 5 || $index_bis < 0) return;
elseif ($index_bis = 5) : echo 'This';
elseif ($index_bis = 1) : echo 'That';
endif;
}
What's wrong with this? I actually can't understand where is the mistake...
Thank you for your help.
What is this? PHP?
And what are you attempting to do?
How about this:
function my_func($index_bis) {
if (!is_int($index_bis) || $index_bis > 5 || $index_bis < 0){
$str = $index_bis;
}
elseif ($index_bis == 5) {
$str='This';
}
elseif ($index_bis == 1) {
$str='That';
}
return $str;
}
It's pretty atypical to put anything after "return"; and I think "return" stops the function, effectively.
Edit: FOREACH method.
If you are building html from two related variables, FOREACH is a great method for handling them, as you can set a data "key" and "value" for each item in the array.
By default, the "key" in PHP is assigned a number, starting from 0 and progressing through increasing integers. You have the option of naming these keys, instead of using the PHP-assigned default.
So consider setting up your array as such:
$newArray = (
'tab name' => 'label name',
'apples' => 'red',
'oranges' => 'orange',
);
Wherein the item to the left of the "=>" is always the key, and the item to the right is always the value.
Then, you can add more elements to this array in your code by using this line:
$newArray['bananas'] = 'yellow';
Note the key (tab) name, with square brackets (because it is the key name) and single quotes (because it is a string).
Now that you have an array, you can "walk" it using a FOREACH loop and echoing the html. Here is the code:
foreach($newArray as $key => $value) {
echo "<tab>$key</tab> : ";
echo "<label>$value</label>
echo "<br /";
}
This will output the html below:
<tab>tab name</tab> : <label>label name</label><br />
<tab>apples</tab> : <label>red</label><br />
<tab>oranges</tab> : <label>orange</label><br />
<tab>bananas</tab> : <label>yellow</label><br />
Very useful. Have fun!
Note: There are likely better ways to do this, but I'm sharing this one as a simple solution. And also, because you shouldn't go further without understanding both FOREACH and simple arrays.
Note: If you want to incorporate more than two variables, you will need a more complex data structure. In PHP, these are called "multidimensional arrays". Give it a read. Beyond that, there are still more methods for handling data (ie. "objects").
I was put in front of this problem when working on a blog post preview list.
They need to shorten the content but not break any html tags by leaving them open.
I have heard that reg ex is not a good option. I am looking for something simple and working.
I appreciate your help in advance as always (SO ended up being a very nice place to come over with problems like that :-)
Wordpress has a function for generating excerpts built-in to the blogging platform which generates an excerpt from the actual blog post.
You didn't specify which language you were looking to use for the trim function so here is the Wordpress version. It can be easily modified and re-purposed to use outside of Wordpress if need be.
wp_trim_words() function reference
/**
* Generates an excerpt from the content, if needed.
*
* The excerpt word amount will be 55 words and if the amount is greater than
* that, then the string ' […]' will be appended to the excerpt. If the string
* is less than 55 words, then the content will be returned as is.
*
* The 55 word limit can be modified by plugins/themes using the excerpt_length filter
* The ' […]' string can be modified by plugins/themes using the excerpt_more filter
*
* #since 1.5.0
*
* #param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
* #return string The excerpt.
*/
function wp_trim_excerpt($text = '') {
$raw_excerpt = $text;
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[…]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
This question relates to an animated map template which we have developed at the UKs Office for National Statistics. It has been applied to many datasets and geographies many uses without problem. For example,
http://www.ons.gov.uk/ons/interactive/vp3-census-map/index.html
http://www.statistica.md/pageview.php?l=ro&idc=390&id=3807
The .fla calls on a supporting .as file (see below) to introduce a thousand separator (in the UK a comma, in Germany a full stop (period) defined elsewhwere.
However, the dataset I am currently mapping has large negative values, and it tutrns out that the ORIGINAL HELPER FUNCTION below does not like negative values with 3, 6, 9 or 12 (etc) digits.
-100 to -999 for instance are rendered NaN,100 to NaN,999.
This is because such values are recognised as being 4 digits long. They are being split, the comma introduced, and the -ve sign is misunderstood.
I reckon the approach must be to use absolute values, add in the comma and then (for the negative values) add the -ve sign back in afterwards. But so far, trials of the ADAPTED HELPER FUNCTION have produced only error. :-(
Can anyone tell me how to put the -ve sign back in , please?
Many thanks.
Bruce Mitchell
==================================================================================
//ORIGINAL HELPER FUNCTION: ACCEPTS A NUMBER AND RETURNS A STRING WITH THOUSANDS SEPARATOR ATTACHED IF NECESSARY
function addThouSep(num) {
/*
a. Acquire the number - 'myTrendValue' or 'myDataValue' - from function calcValues
b. Record it (still as a number) to data precision.
1. Turn dataORtrend into a string
2. See if there is a decimal in it.
3. If there isn't, just run the normal addThouSep.
4. If there is, run addThouSep just on the first bit of the string - then add the decimal back on again at the end.
*/
var myNum:Number = correctFPE(num); // Create number variable myNum and populate it with 'num'
// (myTrendvalue or myData Value from calcValues function) passed thru 'correctPFE'
var strNum:String = myNum+""; // Create string version of the dataORtrend number - so instead of 63, you get '63'
var myArray = strNum.split("."); // Create array representing elements of strNum, split by decimal point.
//trace(myArray.length); // How long is the array?
if (myArray.length==1) { // Integer, no decimal.
if (strNum.length < 4)//999 doesn't need a comma.
return strNum;
return addThouSep(strNum.slice(0, -3))+xmlData.thouSep+strNum.slice(-3);
}
else { // Float, with decimal
if (myArray[0].length < 4)//999 doesn't need a comma
return strNum;
return (addThouSep(myArray[0].slice(0, -3))+xmlData.thouSep+myArray[0].slice(-3)+"."+myArray[1]);
}
}
==================================================================================
//ADAPTED HELPER FUNCTION: ACCEPTS A NUMBER AND RETURNS A STRING WITH THOUSANDS SEPARATOR ATTACHED IF NECESSARY
function addThouSep(num) {
/*
a. Acquire the number - 'myTrendValue' or 'myDataValue' - from function calcValues
b. Record it (still as a number) to data precision.
1. Turn dataORtrend into a string
2. See if there is a decimal in it.
3. If there isn't, just run the normal addThouSep.
4. If there is, run addThouSep just on the first bit of the string - then add the decimal back on again at the end.
*/
var myNum:Number = correctFPE(num); // Create number variable myNum and populate it with 'num'
// (myTrendvalue or myData Value from calcValues function) passed thru 'correctPFE'
var myAbsNum:Number = Math.abs(myNum); // ABSOLUTE value of myNum
var strNum:String = myAbsNum+""; // Create string version of the dataORtrend number - so instead of 63, you get '63'
var myArray = strNum.split("."); // Create array representing elements of strNum, split by decimal point.
//trace(myArray.length); // How long is the array?
if (myNum <0){ // negatives
if (myArray.length==1) { // Integer, no decimal.
if (strNum.length < 4)//999 doesn't need a comma.
return strNum;
return addThouSep(strNum.slice(0, -3))+xmlData.thouSep+strNum.slice(-3);
}
else { // Float, with decimal
if (myArray[0].length < 4)//999 doesn't need a comma
return strNum;
return (addThouSep(myArray[0].slice(0, -3))+xmlData.thouSep+myArray[0].slice(-3)+"."+myArray[1]);
}
}
else // positive
if (myArray.length==1) { // Integer, no decimal.
if (strNum.length < 4)//999 doesn't need a comma.
return strNum;
return addThouSep(strNum.slice(0, -3))+xmlData.thouSep+strNum.slice(-3);
}
else { // Float, with decimal
if (myArray[0].length < 4)//999 doesn't need a comma
return strNum;
return (addThouSep(myArray[0].slice(0, -3))+xmlData.thouSep+myArray[0].slice(-3)+"."+myArray[1]);
}
}
==================================================================================
If you're adding commas often (or need to support numbers with decimals) then you may want a highly optimized utility function and go with straightforward string manipulation:
public static function commaify( input:Number ):String
{
var split:Array = input.toString().split( '.' ),
front:String = split[0],
back:String = ( split.length > 1 ) ? "." + split[1] : null,
pos:int = input < 0 ? 2 : 1,
commas:int = Math.floor( (front.length - pos) / 3 ),
i:int = 1;
for ( ; i <= commas; i++ )
{
pos = front.length - (3 * i + i - 1);
front = front.slice( 0, pos ) + "," + front.slice( pos );
}
if ( back )
return front + back;
else
return front;
}
While less elegant it's stable and performant — you can find a comparison suite at my answer of a similar question https://stackoverflow.com/a/13410560/934195
Why not use something simple like this function I've made?
function numberFormat(input:Number):String
{
var base:String = input.toString();
base = base.split("").reverse().join("");
base = base.replace(/\d{3}(?=\d)/g, "$&,");
return base.split("").reverse().join("");
}
Tests:
trace( numberFormat(-100) ); // -100
trace( numberFormat(5000) ); // 5,000
trace( numberFormat(-85600) ); // -85,600
Explanation:
Convert the input number to a string.
Reverse it.
Use .replace() to find all occurrences of three numbers followed by another number. We use $&, as the replacement, which basically means take all of those occurences and replace it with the value we found, plus a comma.
Reverse the string again and return it.
Did you try using the built in Number formatting options that support localized number values:
Localized Formatting with NumberFormatter
I am trying to pull data from the justin.tv API and store the echo I get in the below code in to a database or a file to be included in the sidebar of website. I am not sure on how to do this. The example of what I am trying to achieve is the live streamers list on the sidebar of teamliquid.net. Which I have done but doing it the way I have done it slows the site way down because it does about 50 json requests every time the page loads. I just need to get this in to a cached file that updates every 60 seconds or so. Any ideas?
<?php
$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=colcatz");
$json_array = json_decode($json_file, true);
if ($json_array[0]['name'] == 'live_user_colcatz') echo 'coL.CatZ Live<br>';
$json_file = file_get_contents("http://api.justin.tv/api/stream/list.json?channel=coldrewbie");
$json_array = json_decode($json_file, true);
if ($json_array[0]['name'] == 'live_user_coldrewbie') echo 'coL.drewbie Live<br>';
?>
I'm not entirely sure how you would imagine this being cached, but the code below is an adaption of a block of code I've used in the past for some Twitter work. There are a few things that could probably be done better from a security perspective. Anyway, this gives you a generic way of grabbing the Feed, parsing through it, and then sending it to the database.
Warning: This assumes that there is a database connection already established within your own system.
(* Make sure you scroll to the bottom of the code window *)
/**
* Class SM
*
* Define a generic wrapper class with some system
* wide functionality. In this case we'll give it
* the ability to fetch a social media feed from
* another server for parsing and possibly caching.
*
*/
class SM {
private $api, $init, $url;
public function fetch_page_contents ($url) {
$init = curl_init();
try {
curl_setopt($init, CURLOPT_URL, $url);
curl_setopt($init, CURLOPT_HEADER, 0);
curl_setopt($init, CURLOPT_RETURNTRANSFER, 1);
} catch (Exception $e) {
error_log($e->getMessage());
}
$output = curl_exec($init);
curl_close($init);
return $output;
}
}
/**
* Class JustinTV
*
* Define a specific site wrapper for getting the
* timeline for a specific user from the JustinTV
* website. Optionally you can return the code as
* a JSON string or as a decoded PHP array with the
* $api_decode argument in the get_timeline function.
*
*/
class JustinTV extends SM {
private $timeline_document,
$api_user,
$api_format,
$api_url;
public function get_timeline ($api_user, $api_decode = 1, $api_format = 'json', $api_url = 'http://api.justin.tv/api/stream/list') {
$timeline_document = $api_url . '.' . $api_format . '?channel=' . $api_user;
$SM_init = new SM();
$decoded_json = json_decode($SM_init->fetch_page_contents($timeline_document));
// Make sure that our JSON is really JSON
if ($decoded_json === null && json_last_error() !== JSON_ERROR_NONE) {
error_log('Badly formed, dangerous, or altered JSON string detected. Exiting program.');
}
if ($api_decode == 1) {
return $decoded_json;
}
return $SM_init->fetch_page_contents($timeline_document);
}
}
/**
* Instantiation of the class
*
* Instantiate our JustinTV class, fetch a user timeline
* from JustinTV for the user colcatz. The loop through
* the results and enter each of the individual results
* into a database table called cache_sm_justintv.
*
*/
$SM_JustinTV = new JustinTV();
$user_timeline = $SM_JustinTV->get_timeline('colcatz');
foreach ($user_timeline AS $entry) {
// Here you could check whether the entry already exists in the system before you cache it, thus reducing duplicate ID's
$date = date('U');
$query = sprintf("INSERT INTO `cache_sm_justintv` (`id`, `cache_content`, `date`) VALUES (%d, '%s', )", $entry->id, $entry, $date);
$result = mysql_query($query);
// Do some other stuff and then close the MySQL Connection when your done
}