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

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

Related

how to print a number in a particular pattern? [duplicate]

This question already has answers here:
MySQL custom primary key generator
(3 answers)
Closed 3 years ago.
I have a pattern of integer (0000012019). where 2019 is current year and 000001 need to increase with every new row inserted to db like 000002, 000003, 000004, 000005 and so on.
how can i achieve this?
First of all it's a string you have.
You can split the string with substr() and do the math on the first part and then convert back to string with str_pad by padding with 0's from left.
$str = '0000012019';
$first = substr($str, 0, 6);
$year = substr($str, -4);
$first = str_pad($first+1, 6, 0, STR_PAD_LEFT);
$new = $first . $year;
echo $new; // 0000022019

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!
}

Remove charactor/digit from string if string length greater than some value [duplicate]

This question already has answers here:
How do you pull first 100 characters of a string in PHP
(6 answers)
Closed 10 months ago.
using PHP, How can i remove the rest of character if sting_Len is greater than 6 for example i have 600275L and i want to end up like 600275 but only if greater than 6 digit.
i used the following code to to extract value starts with 600, i want update it to work for the above condition, Thank you
if((substr($key, 0, 3) == "600") && ($row['ItemClass']==3))
{
$assy = $key;
$rout = "Assy";
}
If you always want to limit it to six characters, then you should just be able to use substr for this without checking the length. If you write:
$string = 'abcdefg';
$string = substr($string, 0, 6);
then $string will equal 'abcdef'.
But if $string is shorter than 6 characters, it will just return the entire string. So if you write:
$string = 'abc';
$string = substr($string, 0, 6);
then $string will equal 'abc'.
You can see this in the PHP manual here.
Use the following logic
if(strlen($code) > 6) { echo substr($code, 0, 6); }

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

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;

Split the long digit into small in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to display number in a certain format in php
I have following digit : 3234.54554545545
I want to split it like 3234.54, means I want to display only 2 char after dot.
Following is something I am trying but its not working
<?php
$v = "3234.54554545";
$pos = strpos($v, '.');
$r = substr($v, count($pos) - 3);
echo $v."".$r;
?>
Actual result: 3234.54554545545
Desired output : 3234.54
Please help me. Thanks
You can use number_format function as:
$v = "3234.54554545";
$r = number_format($v, 2, '.', '');
Why can't you use number_format?
<?php
$v = "3234.54554545";
$r = number_format($v, 2);
?>
PHP is super-flexible with it's types, and numeric strings can be used directly as numbers.
//This is true
('123.45' == 123.45)
As such you can use number_format to format the string. You can also use sprintf if you need more control.

Categories