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, '/'));
Related
This question already has answers here:
extract part of a path with filename with php
(2 answers)
Closed 2 years ago.
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736"
My intention is to have the videos path (stripping the escaped forward slash) and the identifier set to a variable, like videos/585903773a9_654641765275736
Is this possible using str_replace or is this strictly regex required?
Try this Way. I think it will help you. I hope you will get your desire output.
<?php
// Your code here!
$string = "https:\/\/s3.us-west-2.amazonaws.com\/dev-lopes\/videos\/585903773a9_654641765275736";
//remove back slashes
$str1 = stripslashes($string);
//count total string length.
$len = strlen($str1);
//pick the position of /videos
$pos = strpos($str1,'/vid');
//split it from the url.
$break_string = substr($str1, $pos, $len);
//output: /videos/585903773a9_654641765275736
echo($break_string);
?>
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:
PHP remove all characters before specific string
(4 answers)
Closed 8 years ago.
How can I remove everything before the first ABC in a string?
So that this:
somethingrandomABCotherrandomstuffABCmorerandomstuffABC
turns to this:
otherrandomstuffABCmorerandomstuffABC.
Is this possible with php?
There is a php built in for doing this: strstr
Combine with substr to strip out your token:
$out = substr(strstr($text, 'ABC'), strlen('ABC'))
<?php
function removeEverythingBefore($in, $before) {
$pos = strpos($in, $before);
return $pos !== FALSE
? substr($in, $pos + strlen($before), strlen($in))
: "";
}
echo(removeEverythingBefore("somethingrandomABCotherrandomstuffABCmorerandomstuffABC", "ABC"));
?>
Outputs:
otherrandomstuffABCmorerandomstuffABC
This question already has answers here:
PHP remove all characters before specific string
(4 answers)
Closed 8 years ago.
How can I remove everything before the first ABC in a string?
So that this:
somethingrandomABCotherrandomstuffABCmorerandomstuffABC
turns to this:
otherrandomstuffABCmorerandomstuffABC.
Is this possible with php?
There is a php built in for doing this: strstr
Combine with substr to strip out your token:
$out = substr(strstr($text, 'ABC'), strlen('ABC'))
<?php
function removeEverythingBefore($in, $before) {
$pos = strpos($in, $before);
return $pos !== FALSE
? substr($in, $pos + strlen($before), strlen($in))
: "";
}
echo(removeEverythingBefore("somethingrandomABCotherrandomstuffABCmorerandomstuffABC", "ABC"));
?>
Outputs:
otherrandomstuffABCmorerandomstuffABC
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