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

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

Related

How to change all front decimal to 0 in php [duplicate]

This question already has answers here:
Add zero to before decimal point of a number
(2 answers)
Closed 2 years ago.
I have some data from database like this :
43966.31875
how to change 43966 to 0 and keep decimal number to 31875, What should I do?
well, for the final result like this :
0.31875
If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You
The regex method:
$string = '43966.31875';
echo preg_replace('/(\d+).(\d+)/i', '0.${2}', $string);
Will split number into 2 parts then replace the part 1 with "0."
The strstr method:
$string = '43966.31875';
echo '0'.strstr($string, '.');
Also split the number into 2 parts and take the second part and add "0" before
$number = 43966.31875;
$decimalPart = fmod($number, 1);
$decimalCount = explode('.', $number)[1];
$result = round($decimalPart, strlen($decimalCount));
Refer the Official Documentation:
PHP fmod

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

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); }

How to check if string contains just 1 same number [duplicate]

This question already has answers here:
Php Regular Expression repeated characters
(2 answers)
Closed 9 years ago.
I've got a Call Me Back form which sends me a phone number of a person who wants to be called back. Today I received the form with '88888888' instead of a real phone number.
How can I check if the string contains 1 and the same number, continous?
There must not be more than 4 same numbers in a row.
To check if the string contains only one repeated integer, string -> array, check if the unique count is 1.
<?php
$string = "88888888";
$array = array_unique( str_split( $string ) );
$result = $array;
if( count($result) === 1 ) {
echo "Same number repeated in string";
}else{
echo "More than 1 number found in string";
}
?>
-Edit-
optimized: Removed for loop thanks to comment by #Uberfuzzy
$number_string = (string)$number_string;
return strlen($number_string) > 0 && str_repeat($number_string[0], strlen($number_string)) === $number_string;

PHP List all possible 6-digit numbers with specific digits [duplicate]

This question already has answers here:
Algorithm to get all possible string combinations from array up to certain length
(12 answers)
Closed 9 years ago.
How could I generate every possible number of a given amount of digits and using specific digits?
So basically, I would like to have a 6 digit number for example using only the numbers ['1','2','3']. I've tried a few methods of recursion, however, I can't get it to work correctly due to my other complication, which is adding a separator of "|" in between each 2 digits. So the list would be like so:
11|11|11
11|11|12
11|11|13
11|11|21
11|11|22
11|11|23
etc..
Would be appreciated if someone could point me in the right direction.
Also a way of dumping each of the combinations into my MySQL database would be great.
Here is a much updated answer (originally updated from this answer]1) to your question:
function findPermutations($arr, $arrLen, $size, $perArr = array(), $pos = 0, &$found = array()) {
if ($size==$pos) { //if $pos reach $size then we have found one permutation
$found[] = vsprintf("%s%s|%s%s|%s%s", $perArr);
return;
}
for ($i=0; $i<$arrLen; $i++) {
$perArr[$pos] = $arr[$i]; //put i'th char in current position
//The recursive call that move to next position with $pos+1
findPermutations($arr, $arrLen, $size, $perArr, $pos+1, $found);
}
return $found;
}
$permutations = array();
$letters = array('1','2','3');
$max_length = 6;
$permutations = findPermutations($letters, count($letters), $max_length);
for($i = 0; $i < count($permutations); $i++) {
print ($permutations[$i].'<br/>');
}
Here is what I'm doing. I'm passing in an empty array called $permutations by reference, and as I find new permutations, I'm appending them to it. When the function findPermutations() is complete, I end up with an array of all permutation, that I can iterate over or insert. To get the formatting I'm using vsprintf, that lets me pass an array of data and apply a format (in this case %s%s|%s%s|%s%s). Lastly I'm using default argument values to make calling this function cleaner.
you mean something like this?
$letters='123'; // add other numbers
for($i=0;$i<3;$i++) { //generate 3 pairs
$pairs[]=$letters[rand(0,2)] . $letters[rand(0,2)];
}
//then join them together
$finalstring=implode('-',$pairs);

Categories