How to add value substring inside decimal in PHP - php

I have a number like this
"0.321527778"
I want make some substring from my value in top and then for the substring result I want input that number into the my decimal number, how can I do? (for substring I cut from 2 last digits from decimal number)
for the expetation result will be like this :
"0.321527777777778"
code :
<?php
$number = "0.321527778";
$substring = substr($number, -2, -1);
print_r($number . ' ' . $substring);
?>
result :
0.321527778 7
If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You

You need to get the substrings before and after the part that you want to insert multiple times. Then concatenate everything without putting spaces between them.
$number = "0.321527778";
$beginning = substr($number, 0, -2);
$end = substr($number, -1);
$substring = substr($number, -2, -1);
$result = $beginning . str_repeat($substring, 6) . $end;
echo $result;

Related

Finding a character at a specific position of a string

Since I am still new to PHP, I am looking for a way to find out how to get a specific character from a string.
Example:
$word = "master";
$length = strlen($word);
$random = rand(1,$length);
So let's say the $random value is 3, then I would like to find out what character the third one is, so in this case the character "s". If $random was 2 I would like to know that it's a "a".
I am sure this is really easy, but I tried some substr ideas for nearly an hour now and it always fails.
Your help would be greatly appreciated.
You can use substr() to grab a portion of a string starting from a point and going length. so example would be:
substr('abcde', 1, 1); //returns b
In your case:
$word = "master";
$length = strlen($word) - 1;
$random = rand(0,$length);
echo substr($word, $random, 1);//echos single char at random pos
See it in action here
You can use your string the same like 0-based index array:
$some_string = "apple";
echo $some_string[2];
It'll print 'p'.
or, in your case:
$word = "master";
$length = strlen($word);
$random = rand(0,$length-1);
echo $word[$random];
Try this simply:
$word = "master";
$length = strlen($word);
$random = rand(0,$length-1);
if($word[$random] == 's'){
echo $word[$random];
}
Here I used 0 because $word[0] is m so that we need to subtract one from strlen($word) for getting last character r
Use substr
$GetThis = substr($myStr, 5, 5);
Just use the same values for the same or different if you want multiple characters
$word = "master";
$length = strlen($word);
$random = rand(0,$length-1);
$GetThis = substr($word, $random, $random);
As noted in my comment (I overlooked as well) be sure to start your rand at 0 to include the beginning of your string since the m is at place 0. If we all overlooked that it wouldn't be random (as random?) now would it :)
You can simply use $myStr{$random} to obtain the nth character of the string.

Convert numbers to time, or in other words, insert : in the middle of a string

how would I be able to inser a : in the middle of numbers like 840 or 1530. I basically want to add a : at the third last position, as I got some strings with 3 and some with 4 numbers in it.
Thanks!
Try this:
$x = '1930';
echo substr($x, 0, strlen($x) - 2) . ':' . substr($x, strlen($x) - 2);
Here is another way:
<?php
$int = 1530;
$str = (string) $int;
$newstring = substr_replace($str, ':', 3, 0);
echo $newstring;
?>

Adding a string and an integer in php

I'm trying to add a 1 in front of my binary code and this is how I'm going about it:
if I have 0101, for example, then I'd add a number with 4 zeroes, like 10000 so it would become 10101. Here's my code:
$fill = strlen($string);
$number = '1';
$add = str_pad($number, $fill, '0', STR_PAD_RIGHT);
$m1 = $string + $add;
The problem is the output for that is something like 1.random number e+Random number
assuming $string is your "0101" string, you could just do $m1 = '1'.$string;
My previous answer was wrong because the length of the string is potentially variable and str_pad requires you to know the length. This will work, but it doesn't look so elegant:
if (strpos($string, '0') === 0) {
$string = '1' . $string;
}

how to remove more 1 decimal numbers on a number

What's the simple way to remove more than 1 decimal number from source number .
for example source numbers are :
1st source number is : 56.48216585224
2nd source number is: 93
Output must be :
1st output : 56.4
2nd output: 93
numbers are not static
what's the simple way ?
If you don't want rounding, then:
$number = 56.48216585224;
echo substr($number, 0, strpos($number, '.')+2); // output: 56.4
Otherwise:
Use php round() or number_format()
http://php.net/manual/en/function.round.php
http://php.net/manual/en/function.number-format.php
Examples:
$number = 56.48216585224;
echo number_format($number, 1, '.', ''); // Output: 56.5
echo round($number, 1); // Output: 56.5
I will suggest this PHP code for your requirement:
$n = 56.48216585224;
$m = floor($n * 10) / 10; // $m = 56.4 now
You can try the following
print(quickFormat("56.48216585224"));
print(quickFormat("93"));
function quickFormat($number) {
$number = explode(".", $number);
if (isset($number[1])) {
return $number[0] . "." . $number[1]{0};
} else {
return $number[0] ;
}
}
Output
56.4
93

How to extract substring by start-index and end-index?

$str = 'HelloWorld';
$sub = substr($str, 3, 5);
echo $sub; // prints "loWor"
I know that substr() takes the first parameter, 2nd parameter is start index, while 3rd parameter is substring length to extract. What I need is to extract substring by startIndex and endIndex. What I need is something like this:
$str = 'HelloWorld';
$sub = my_substr_function($str, 3, 5);
echo $sub; // prints "lo"
Is there a function that does that in php? Or can you help me with a workaround solution, please?
It's just math
$sub = substr($str, 3, 5 - 3);
The length is the end minus the start.
function my_substr_function($str, $start, $end)
{
return substr($str, $start, $end - $start);
}
If you need to have it multibyte safe (i.e. for chinese characters, ...) use the mb_substr function:
function my_substr_function($str, $start, $end)
{
return mb_substr($str, $start, $end - $start);
}
Just subtract the start index from the end index and you have the length the function wants.
$start_index = 3;
$end_index = 5;
$sub = substr($str, $start_index, $end_index - $start_index);
You can just use a negative value on the third parameter:
echo substr('HelloWorld', 3, -5);
// will print "lo"
If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative).
As stated at the substr documentation.
Not exactly...
If we have a start index as 0, and we want JUST the first char, it becomes difficult as this will not output what you want. So if your code is requiring an $end_index:
// We want just the first char only.
$start_index = 0;
$end_index = 0;
echo $str[$end_index - $start_index]; // One way... or...
if($end_index == 0) ++$end_index;
$sub = substr($str, $start_index, $end_index - $start_index);
echo $sub; // The other way.

Categories