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);
?>
Related
This question already has answers here:
ltrim strips more than needed
(5 answers)
Closed 3 years ago.
I want to strip the url and the trailing characters to extract just the id in the url. However, it is removing the first character of the id if it is in the trim string.
Example: https: //website.com/profile/w300u-87wz?utm_source=ab&utm_medium=email
I want to return w300u-87wz but it is returning 300u-87wz without the first "w".
If the id starts with any letter in the string "https: //website.com/profile/" it strips that letter at the beginning, only.
If it does not contain a letter in the string "https: //website.com/profile/" it works fine.
Example: https: //website.com/profile/d300u-87wz?utm_source=ab&utm_medium=email
It does return d300u-87wz
if($platformsearch == "category_website"){
$str = $searchuser2;
$str2 = ltrim($str, "https://www.website.com/profile/");
$str3 = strtok($str2, '?');
$searchuser2 = $str3;
}
I have tried ltrim() and trim().
How can I get it to strip the exact string and keep the rest?
UPDATE...
I can use str_replace() will do the trick and even further if viewed from an "about" email.
If link is retrieved directly or from about email section:
if($platformsearch == "category_website"){
$str = $searchuser2;
$str2 = str_replace("https://www.website.com/profile/", "", $str);
$str3 = str_replace("about/", "", $str2);
$str4 = strtok($str3, '?');
$searchuser2 = $str4;
}
Example: https: //website.com/profile/w300u-87wz?utm_source=ab&utm_medium=email
Will return "w300u-87wz"
It will then continue to run through if from about email to remove "about/"
Example: https: //website.com/profile/about/w300u-87wz?utm_source=ab&utm_medium=email
Will return "w300u-87wz"
However, in seeing answer, below (marked as correct answer) using parse_url() is the cleanest and easiest option for stripping a url!
A mix of parse_url() and explode() and end() would be one way to skin this particular cat.
$s = 'https: //website.com/profile/w300u-87wz?utm_source=ab&utm_medium=email';
$a = parse_url($s);
$bits = explode('/', $a['path']);
echo end($bits);
RESULT
w300u-87wz
This question already has answers here:
PHP rtrim ".php"
(3 answers)
Closed 4 years ago.
Why:
$string = 'foofoxoa';
$toCut = 'xoa';
rtrim($string, $toCut);
return me:
foof
instead of:
foofo
? I don't want to use str_replace or substr for this, because $string and $cut can have different values.
In addition, $cut can be twice in $string, and I want to cut only from the end.
It is because o is in the list of characters to remove from the end.
I think rtrim works its way from end to start and once it reaches a character not in the list it stops.
Example:
php > $string = 'foofolxoa';
php > $toCut = 'xoa';
php > echo rtrim($string, $toCut);
foofol
https://secure.php.net/manual/en/function.rtrim.php#35108
This question already has answers here:
removing #email.com from string in php
(6 answers)
Closed 4 years ago.
How can I use the str_replace() without using an array of words for turning:
some#mail.com
into
some
So, everything after the '#' sign includes # as well.
How can I do that?
As an example, i will type 'adminofsite#xxxwwweeerandomstuff.com'
and the output will be: 'adminofsite'.
use strstr
$email="john#doe.com"
$user = strstr($email, '#', true);
echo $user;
$str = "some#mail.com"
$str = substr($str,0,strpos($str,"#"))
function stripEmailDomain($string){
return substr($string, 0, strpos($string, '#'));
}
I exploded and then rebuilt the string. This will work if you are positive the string will always be an email address. Its a work around but seems flexible if you want to modify it for a specific purpose.
<?php
$explo0= explode('#',"some#mail.com");
for($i=0;$i<count($explo0);$i++){
$exploresult0.=$explo0[$i+1];
}
echo "#".$exploresult0;
?>
This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);
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.