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