printing out 1 random letter of a word php functions [duplicate] - php

This question already has answers here:
a method of selecting random characters from given string
(15 answers)
Closed 8 years ago.
i want to Use strlen(), substr(), and rand() to print a random character from my name to the screen.
<html>
<p>
<?php
// Use strlen(), substr(), and rand() to
// print a random character from my name to the screen.
$name = "jordi";
$length = strlen("$name");
$length = rand();
$partial = substr($lengt, 0,5);
print $name. "<br />";
print $length. "<br />";
print $partial. "<br />";
?>
</p>
</html>
the outcomes right now(the numbers are randomly generated ofcourse):
jordi
9286122
92861
can someone help me with this.

Here are the steps using all of the functions required by your assignment -
$nameLength = strlen($name); // gets the length of the name
$randomNumber = rand(0, $nameLength - 1); // generates a random number no longer than the name length
$randomLetter = substr($name, $randomNumber, 1); // gets the substring of one letter based on that random number
echo($randomLetter);

$name = "jordi";
$rndCharIndex = rand(0, strlen($name) - 1); // random character index for the string
$rndChar = $name[$rndCharIndex]; // Look up the character in that random index
echo $rndChar;

Related

Is there any way to regex and delete after a certain point in a decimal number in PHP? [duplicate]

This question already has answers here:
PHP How do I round down to two decimal places? [duplicate]
(16 answers)
Closed 4 years ago.
Is there any way to do a regex that cuts off a number at a certain point without rounding (simply drops the digits?) say after 4 digits.... It will not be handling negative numbers, EVER. I could have number inputs such as 0.03123 or 1.31, or 10000.98, etc .... What I have written so far as my solution is rounding and not what I'm seeking....
$number = 10000.51999999;
$precision = 4;
echo "<br>";
// grab number before decimal by rounding down the whole number down...
$numberBeforeDecimal = floor($number);
echo "<br>";
// grab the decimal and set the correct precision needed
$n = $number;
intval($n); // 12
$theDecimalPart = explode('.', number_format($n, ($precision)))[1]; // 3430
echo $theDecimalPart; // this is outputting 5200
$theNewValue = $numberBeforeDecimal.".".$theDecimalPart;
explode() the number to get integer and decimal part separated out in an array
Use substr() function to get relevant precision from the decimal part.
Finally, concatenate them back.
Try the following (Rextester DEMO):
$number = 10000.51999999;
$precision = 4;
// separate out the integer and decimal part
$number_str_arr = explode('.', $number);
// concatenate them back
$theNewValue = $number_str_arr[0] . '.' . substr($number_str_arr[1], 0, $precision);
echo $theNewValue; // displays 10000.5199

Printing a random letter from your name in PHP [duplicate]

This question already has answers here:
a method of selecting random characters from given string
(15 answers)
Closed 6 years ago.
I just start to learn PHP, but I can't to make a easy problem. Can you help me and after say why my problem doesn't work? Here is the problem:
Create a new variable $name and store your name in it.
Then print a random character from your name. Use your knowledge of strlen(string), rand(min, max), and substr(string, start, length) to do this.
HINT: Remember that substr() treats characters in a string as a zero-indexed array (first letter is at position zero). This means that the last character in the string will be at position length - 1.
I try but without result.
<html>
<p>
<?php
$name = "George";
$fl = substr($name, 0, strlen($name));
$sl = substr($name, 0, 1);
print rand($fl,$sl);
?>
</p>
</html>
I think problem is at rand...I can't randomise items with id or something?I can print the $sl or $fl ..
It can be done like this:
<?php
$name = "George";
$nameLength = strlen($name);
$randomNumber = rand(0, $nameLength - 1);
$randomLetter = substr($name, $randomNumber, 1);
echo $randomLetter;
?>
Source: https://stackoverflow.com/a/23915981/5798798

How do I get the first and the last digit of a number in PHP?

How can I get the first and the last digit of a number? For example 2468, I want to get the number 28. I am able to get the ones in the middle (46) but I can't do the same for the first and last digit.
For the digits in the middle I can do it
$substrmid = substr ($sum,1,-1); //my $sum is 2468
echo $substrmid;
Thank you in advance.
You can get first and last character from string as below:-
$sum = (string)2468; // type casting int to string
echo $sum[0]; // 2
echo $sum[strlen($sum)-1]; // 8
OR
$arr = str_split(2468); // convert string to an array
echo reset($arr); // 2
echo end($arr); // 8
Best way is to use substr described by Mark Baker in his comment,
$sum = 2468; // No need of type casting
echo substr($sum, 0, 1); // 2
echo substr($sum, -1); // 8
You can use substr like this:
<?php
$a = 2468;
echo substr($a, 0, 1).substr($a,-1);
You can also use something like this (without casting).
$num = 2468;
$lastDigit = abs($num % 10); // 8
However, this solution doesn't work for decimal numbers, but if you know that you'll be working with nothing else than integers, it'll work.
The abs bit is there to cover the case of negative integers.
$num = (string)123;
$first = reset($num);
$last = end($num);

How to apply rand() or some other built-in function on an array that contains all string elements in order to randomly select a string?

I've a following array :
$family = array("Amit", "Suresh", "Vinit", "Somesh", "Sagar", "Shriram");
Now I want to randomly select a name from the above array.
How should I do this?
I know about rand() function. Even I tried rand($family); but it gave me Warning as below :
Warning: rand() expects exactly 2 parameters, 1 given in /var/www/project/index.php on line 7
It's asking for second argument.
So please someone help me in selecting a string randomly from an array that contains all string elements.
Thanks.
Read the docs first, there is exact function you are looking for in example
http://php.net/manual/en/function.array-rand.php
<?php
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>
Try this
$num = rand(0, count($family)-1);
$family[$num]
The rand() function takes either zero or two arguments. With zero arguments it will produce a pseudo random integer between 0 and getrandmax(). Otherwise it expects two integer arguments min and max and will produce a pseudo random integer between min and max inclusive.
As #Yurich pointed out this is what you probably want.
$num = rand(0, count($family-1));
$family[$num]
Explanation: rand(0, count($family-1)) will produce a random integer between 0 and the number of elements in your array minus one. This number will be stored in $num, which is used afterwards to access the array at a "random" index.
// Count the elements
$count = count($family);
// decrement the count by 1
$count--;
// echo a random item from the array
echo $family[rand(0, $count)];
rand() is not a good option, and for PHP 7 there is a better option. See below:
$family = array("Amit", "Suresh", "Vinit", "Somesh", "Sagar", "Shriram");
if(version_compare(PHP_VERSION,'7.0.0', '<') ) {
// for PHP < 7
$rand_name = $family[mt_rand(0, count($family) - 1)];
} else {
// for PHP >= 7
$rand_name = $family[random_int(0, count($family) - 1)];
}
$num = rand(0, count($family)-1);
$family[$num]

PHP First Letter Uppercase of Each Word In Output [duplicate]

This question already has answers here:
Make all words lowercase and the first letter of each word uppercase
(3 answers)
Closed 1 year ago.
I am wanting the output to have the First Letter Of Each Word uppercase. Here is my code.
function random_title ()
{
$quotes1 = file ("wp-content/plugins/includes/classes/quotes.txt", FILE_IGNORE_NEW_LINES);
$quotes1 = ucwords($quotes1);
$num = rand (0, intval (count ($quotes1) / 3)) * 3;
return $quotes1[$num];
}
Usage is:
random_title()
This part is not working, what am I doing wrong? I get no output when I put this in, but if I take it out I do get my titles but they are lowercase as they are in the text file.
$quotes1 = ucwords($quotes1);
Thank you for your help.
ucwords works on a single string, not an array. Just apply it after selecting a random title:
function random_title ()
{
$quotes1 = file ("wp-content/plugins/includes/classes/quotes.txt", FILE_IGNORE_NEW_LINES);
$num = rand (0, intval (count ($quotes1) / 3)) * 3;
return ucwords($quotes1[$num]); # Here!
}

Categories