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);
Related
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:
Extract floating point numbers from a delimited string in PHP
(6 answers)
Closed 6 years ago.
I am trying to extract any number out from a string (with or without decimal) and dump that into an array. Below is my code
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/\d+/",$str,$matches);
echo var_dump($matches);
The current output has 3 elements below:
21
11
30
But I expect to output only 2 elements below:
21.11
30
How do I change my regex?
You should use this regex (add dot in character class) :
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/[\d\.]+/",$str,$matches);
var_dump($matches);
Test it (Ctrl + Enter)
This question already has answers here:
How to remove anything in a string after "-"?
(7 answers)
Closed 7 years ago.
i've a query string like this:
SELECT * FROM `backplanechanneldecoder` WHERE Pair = '3' LIMIT 25,25
and i want to remove all the text which comes after LIMIT.
Note: This string is generated dynamically from another code on which I've no control that's why I've to remove part of string from my end.
Use str_pos to find the word LIMIT and then strip after that using substr
$string = 'SELECT * FROM `backplanechanneldecoder` WHERE Pair = 3 LIMIT 25,25';
$limit_pos = strpos($string, 'LIMIT');
$string = substr($string, 0, $limit_pos);
echo $string;
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:
Removing the last character of a string IF it is $variable [duplicate]
(5 answers)
Closed 12 months ago.
I am generating a string dynamically. For example The string looks like this
$string = "this-is-a-test-string-for-example-";
It can also be like this
$string = "this-is-a-test-string-for-example";
I want if there is a hyphen "-" at the end of the string, It should be removed. How can I do that in php or regex?
http://pl1.php.net/trim - that function gets characters to trim as last parameter
trim($string,"-");
or as suggested for right side only
rtrim($string,"-");
If it always at the end (right) of the string this will work
$string = rtrim($string,"-");
$cleanedString = preg_replace('/^(this-is-a-test-string-for-example)-$/', '$1', $string);
$delete = array('-');
if(in_array($string[(strlen($string)-1)], $delete))
$string = substr($string, 0, strlen($string)-1);
You can add other characters to delete into $delete array.