Get the first 5 and last 2 chars of a string [duplicate] - php

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

Related

How to add - in-between strings [duplicate]

This question already has answers here:
Insert string at specified position
(11 answers)
Closed 3 years ago.
How do I add - in between strings.
Let’s say for instance, I want to add - in 123456789101 at the fourth position three times thereby making it look like this : 1234-5678-9101.
Substr_replace() or str_replace has not solved the problem.
I would use combination of str_split and implode.
$code = 123456789101;
$formatted = implode('-', str_split($code, 4) );
echo $formatted; //1234-5678-9101
There are a number of ways to do this.
Use substr
$output = sprintf('%s-%s-%s', substr($string, 0,4), substr($string,4,4), substr(8,4));
Use preg_replace
$output = preg_replace('/(.{4,4})(.{4,4})(.{4,4})/', '$1-$2-$3', $string);

Exploding a number and separate it by a space [duplicate]

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.

How to get the last 4 characters in a string - Laravel [duplicate]

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

php cut string by character [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php string function to get substring before the last occurrence of a character
i have string
'storage/product/home_decor/living_room/livingroom/7ce3da03b6854283bdd0b8e33b3e556a.11.09.2012.03.42.33.jpg'
how do I cut the last string by / with php ?
i want my output is
'storage/product/home_decor/living_room/livingroom/'
If you want to keep that last / with your result, use this code:
$string = 'storage/product/home_decor/living_room/livingroom/7ce3da03b6854283bdd0b8e33b3e556a.11.09.2012.03.42.33.jpg';
$min = substr($string, 0, strrpos($string, '/') + 1);
echo $min; //output : storage/product/home_decor/living_room/livingroom/
substr($string, 0, strrpos($string, '/'));

Set string to a specified length in PHP [duplicate]

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

Categories