This question already has answers here:
Zero-pad digits in string
(5 answers)
Closed 11 months ago.
I need to have a string that has a specified length and replace the excess characters with a letter.
e.g.
My original string is : "JOHNDOESMITH". The length should be up to 25 characters only.
I need my string to become "XXXXXXXXXXXXXJOHNDOESMITH" (13 X's and 12 chars from the original string).
Anybody please tell me how to achieve this? Is there a string function for this? I've been racking my brains out for quite some time now and I still can't find a solution.
You could use str_pad() to do it...
echo str_pad($str, 25, 'X', STR_PAD_LEFT);
CodePad.
You could use str_repeat() to do it...
echo str_repeat('X', max(0, 25 - strlen($str))) . $str;
CodePad.
The length should be up to 25 characters only.
You can always run substr($str, 0, 25) to truncate your string to the first 25 characters.
We can use printf() or sprintf() function.
$format= "%'X25s";
printf($format, "JOHNDOESMITH"); // Prints a formatted string
$output = sprintf($format, "JOHNDOESMITH"); // Returns a formatted string
Use the str_pad function:
$a="JOHNDOESMITH";
$b=str_pad($a,25,'X',STR_PAD_LEFT);
print_r($b);
Related
This question already has answers here:
PHP and preg_match Regex to pull number with decimal out of string
(4 answers)
Closed 4 years ago.
I am trying to extract digits and decimal point from a string but the decimal point is lost when I am using following regular expression:
<?php
$str = "$40.0000";
echo $str;
echo "<br />";
$pattern = "/\D+/";
$str = preg_replace($pattern, '', $str);
echo $str;
?>
Output:
$40.0000
400000
I want to retain dot also. How to include dot in my regex?
<?php
$str = '$40000.00';
echo preg_replace('/[^\d.]/','',$str);
Several good answers here, too: How do I convert output of number_format back to numbers in PHP?
There are two appropriate functions for this purpose. The first, round(), rounds a value to a specified number of decimal places. The function’s first argument is the number to be rounded. This can be either a number or a variable with a number value. The second argument is optional; it represents the number of decimal places to round to. For example:
round (4.30); // 4
round (4.289, 2); // 4.29
$num = 236.26985;
round ($num); // 236
The other function you can use in this situation is number_format(). It works like round() in that it takes a number (or a variable with a numeric value) and an optional decimal specifier. This function has the added benefit of formatting the number with commas, the way it would commonly be written:
number_format (428.4959, 2); // 428.50
number_format (428, 2); // 428.00
number_format (123456789); // 123,456,789
This question already has answers here:
Insert string at specified position
(11 answers)
separate string in two by given position
(8 answers)
Closed 4 years ago.
I want to explode a string or an integer and separate it by a space.
E.g., I have this int 12345678, and I want its numbers to become like 123 45678. I want the first three numbers separated. Can someone give me a clue or hint in how to achieve this, like what function to use in PHP? I think using an explode will not work here because the explode function needs a separator.
You can use substr_replace() - Replace text within a portion of a string.
echo substr_replace(1234567, " ", 3, 0); // 123 4567
https://3v4l.org/9CFlX
You could use substr() :
$str = "12345678" ;
echo substr($str,0,3)." ".substr($str, 3); // "123 45678"
Also works with an integer :
$int = 12345678 ;
echo substr($int,0,3)." ".substr($int, 3); // "123 45678"
This problem will solve by using substr().
The substr() function returns a part of a string.
Syntax: substr(string,start,length)
Example:
$value = "12345678";
echo substr($value,0,3)." ".substr($value, 3);
Output: 123 45678
You may get better understand from here.
This question already has answers here:
How can I get the last 7 characters of a PHP string?
(8 answers)
Closed 5 years ago.
I have this string in my database like 12-12-2067. I want to only display the last 4 words in the string 2067 in my view. How do i achieve this ?
$get_string = Item::all()->first();
<p>{{get_string}}</p>
You can use:
substr ($getstring->string, -4)
Because of (-) it will start form the end of the string and it will take the next 4 characters.
Take care that first() it will return an object and not the string you want.
Please read the manual substr.
$str = substr($str, -4);
string substr ( string $string , int $start [, int $length ] )
$dynamicstring = "12-07-2017";
$newstring = substr($dynamicstring, -4);
This question already has answers here:
Truncate a string to first n characters of a string and add three dots if any characters are removed
(20 answers)
Closed 9 years ago.
I have a string like this: $str="moolti1" and I want this to be cut into two strings or something like that.
I need "m00lti" and "1". As the first part is always the same length (6), isn't there a method like $pre=$str[0:5]?
$str="moolti1";
$parts = str_split($str, 6);
$str="moolti1";
$start = substr($str, 0, 6);
$end = substr($str, 6);
You may find I have a number out somewhere as I always ALWAYs get confuzzled with my start indexes and lengths but this should give you what you need.
Use PHP's substr() function
$str = 'moolti1';
$first_five = substr($str, 0, 5);
$last_two = substr($str, -2);
More info here
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Format number in PHP
I want my number format 12 char in length, like if there is value 12 it should output 000000000012
Example: if value is 123.50 it should output 000000012350 No decimal (multiple of 100)
Any idea, which function will be used?
Thanks !
You can use str_pad
echo str_pad(12, 12, "0", STR_PAD_LEFT);
You can also use printf or sprintf
printf("%012s", 12);
Output
000000000012
Some Work Arounds
var_dump(formatOutput("12"));
var_dump(formatOutput("123.50"));
var_dump(formatOutput("123.378201"));
function formatOutput($no,$max = 15) {
$no = str_pad("1", strlen(substr(strrchr($no, "."), 1)), "0", STR_PAD_RIGHT) * $no;
if(strpos($no, "."))
$no = str_replace(".", "", $no) . "0" ;
return str_pad($no, $max, "0", STR_PAD_LEFT);
}
Output
string '000000000000012' (length=15)
string '000000000001235' (length=15)
string '000001233782010' (length=15)
See str_pad() http://php.net/manual/en/function.str-pad.php
string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )
This functions returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.
input
The input string.
pad_length
If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place.
pad_string
The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length.
pad_type
Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
sprintf();
Check php manual sprintf() function in php.net
Along with printf()can do the job. Check example 7 in the link above.