New to our community ?

Discover a world of possibilities! Join us and explore a vibrant community where ideas flourish and connections thrive.

One of Our Valued Members

Thank you for being part of our community. Your presence enriches our shared experiences. Let's continue this journey together!

Home Blog Page 9

How to Convert 1000 to 1k |using PHP

0

Welcome back to shorltearner.com, in our previous post we learn how to Convert words to numbers with the help of PHP. so in this post today we will learn how to convert number into sort number format in PHP.
so before start this tutorial we take an overview on number_format which is also a predefined PHP function that are used to The number_format() function formats a number with grouped thousands.

how to convert number into sort form in php

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

for example if i take the below code as an example

<?php echo number_format("1000000"); ?>

than the number_format function returns an output like

outout : 1,000,000

so If we want to convert number into sort form in thousand, million, billion then we used another predefined PHP function number_format_short that will help to convert any number to it’s sort form like (Eg: 1.4K, 14.32M, 40B ) etc.
so in the below code we take an example which converts a number 1,43,20000 into 14.32M.

<?php 
    function number_format_short($n) {
        // first strip any formatting;
        $n = (0+str_replace(",", "", $n));
        // is this a number?
        if (!is_numeric($n)) return false;
         // now filter it;
        if ($n > 1000000000000) return round(($n/1000000000000), 2).'T';
        elseif ($n > 1000000000) return round(($n/1000000000), 2).'B';
        elseif ($n > 1000000) return round(($n/1000000), 2).'M';
        elseif ($n > 1000) return round(($n/1000), 2).'K';
 
        return number_format($n);
    }
echo number_format_short('14320000'); //14.32M

?>

Upload a file to the Server using|PHP

0

welcome back to shortlearner.com , in our previous post we learn how to Convert words to numbers in PHP. in this post we will see how to Upload a file to the Server with the help of PHP.
Let’s go through the overview of this program before starting the tutorial. So we will first create a file with the name index.php, in this file we are creating a responsive form by using bootstrap classes and taking a file from user and after submitting the form we will upload it on the server.

Upload a file to the Server

Index.php

<html>
 <head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"><!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
 </head> 
 <body> 
<form method="post"   action="index.php" enctype="multipart/form-data">
 Resume : <input type="file" name="f1" class="form-control"> <br>
<input type="submit" value="Upload" class="btn btn-success">
</form> 
</body> 
</html>

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

after submitting the form we will upload this file on the server using below code.

<?php 
if(is_uploaded_file($_FILES['f1']['tmp_name'])) {  
$fname = $_FILES['f1']['name'];            if(move_uploaded_file($_FILES['f1']['tmp_name'],"uploads/$fname"))
{   
echo "Uploaded successfully";
 } 
else
 {  
echo "Not uploaded";
 }
 } 
?>

Get YouTube video thumbnail using PHP

0

welcome back to shortlearner.com , in our previous post we learn how to convert YouTube URL to embed URL PHP.in this post we will see how to Get YouTube video thumbnail and use it with PHP
so in the below code we explode the URL and create a link which will show the thumbnail of individual video id and use it on img tag.

How to get YouTube Video Thumbnail

Also Read :
How to Install PHP on CentOS.
How to integrate Razorpay Payment Gateway using PHP.

Get Domain name from URL
Unable to create a directory a wordpress error
How to Send Attachment on mail using PHP.
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
$text = "https://www.youtube.com/watch?v=74WSXAFXoDk";
$video_id = explode("?v=", $text);
$video_id = $video_id[1];
$thumbnail="http://img.youtube.com/vi/".$video_id."/maxresdefault.jpg";
echo "<img src='$thumbnail'>";
?>

Convert a youtube video url to embed code

0

welcome back to shortlearner.com , in our previous post we learn how to Convert words to numbers in PHP. in this post we will see how to Convert a youtube video url to embed code with the help of PHP.
so in the below code we create a PHP function convertYoutube and pass YouTube link as a parameter, the convertYoutube function is converts our URL into embed code.

convert youtube url to embed url php

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
function convertYoutube($string) {
	return preg_replace(
		"/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
		"<iframe width=\"420\" height=\"315\" src=\"//www.youtube.com/embed/$2\" allowfullscreen></iframe>",
		$string
	);
}
$text = "https://www.youtube.com/watch?v=74WSXAFXoDk";
echo convertYoutube($text); ?>

generate an array with a range using PHP

0

Welcome back to shorltearner.com, we are starting a new series of PHP interview questions for beginners and experienced,so in previous post we learn, Converting words to numbers in PHP
In this post we see how to generate an array with a range taken from a string.

How to generate an array of strings numbers with a specific range

Also Read
Unable to create a directory a wordpress error
PHP program that multiplies corresponding elements of two given lists
PHP program to print out the multiplication table up to 6*6
Write a PHP program to check if the bits of the two given positions of a number are same or not

 <?php
function string_range($str1) 
{
  preg_match_all("/([0-9]{1,2})-?([0-9]{0,2}) ?,?;?/", $str1, $a);
  $x = array ();
  foreach ($a[1] as $k => $v) 
  {
    $x  = array_merge ($x, range ($v, (empty($a[2][$k])?$v:$a[2][$k])));
  }
  return ($x);
}
$test_string = '1-2 18-20 9-14';
print_r(string_range($test_string));
?> 

Output

Array ( [0] => 1 [1] => 2 [2] => 18 [3] => 19 [4] => 20 [5] => 9 [6] => 8 [7] => 7 [8] => 6 [9] => 5 [10] => 4 )

another way to find range between to given numbers with the help of php predefined function range(), see the below example

<?php
$number = range(0,50,10);
print_r ($number);
?>

Output

Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 )