The rtrim() returns a value and does not modify the original value. Please use it this way:
$xcount = rtrim($xcount, ",");
From the rtrim():
Answer from Praveen Kumar Purushothaman on Stack OverflowThis function returns a string with whitespace (or other characters) stripped from the end of str.
The rtrim() returns a value and does not modify the original value. Please use it this way:
$xcount = rtrim($xcount, ",");
From the rtrim():
This function returns a string with whitespace (or other characters) stripped from the end of str.
rtrim() for me is not working. I do believe that it is related to the fact that I use greek characters. Check regex instead.
PHP rtrim ".php" - Stack Overflow
string - PHP rtrim not removing trailing \n - Stack Overflow
Problem with rtrim function
PHP rtrim function trim extra character? Why? - Stack Overflow
rtrim accepts a list of characters as the second argument, so in this case, it will trim not just the .php extension, but any ., p, or h characters found in the rest of the string.
Try using preg_replace("/(.+)\.php instead, or 1", $filename);
basename($filename, '.php') if you have the file on the server, not just in a string.
The second argument to rtrim is a string with a list of characters. In this case, it will strip off any P, H, and . in your string, so returning searc.
It looks like you have the literal characters \n at the end. trim() won't remove these, as they're not whitespace.
Looks like something like this would work...
$str = preg_replace('/(\\\n)+\z/', '', $str);
CodePad.
We've several problems here.
The \n is NOT a single character, as the rtrim is not working.
However, you've to search for a STRING and not a single character. Thats why double quotes should be used while searching the \n.
The second problem, is that as you can see in the question, there are two \n at the end, and might there be more.
What I'd do, is manually program a function that does the following:
- Reverse the string
- Loop while 2 first characters == "n\".
- Replace those two characters for a blank.
- End loop
Q1. Is it possible to use rtrim in my situation?
Yes, it is possible to use rtrim() in your situation.
Q2. Does it depend on php version? No
You just need to use echo rtrim('4dbb3dca&', '&'); not the & because 2nd parameter of rtrim works like below-
You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped
DEMO: https://3v4l.org/anFIR
If you use this below example, then you'll get the same result because it'll try to remove all the given character(s) from the right side of your given string. For Ex-
echo rtrim('4dbb3dca&;pm', '&');` // will also return 4dbb3dc
The second string you specify in rtim() contains all the extra characters you want to trim. So the chars getting trimmed are "&" and "a" (both are indeed included in the second parameter of your rtrim() function call, "&").
To get the result that you want, you should invoke rtrim() this way:
rtrim('4dbb3dca&', '&')
Function seems simple enough:
$myteststring = "php#isretarded";
echo rtrim($myteststring, "#");
output: php#isretarded
Why?
To get the output I want, I have to write:
echo substr($myteststring, 0, strpos($myteststring, "#"));
First, trim() takes arguments in the opposite order: $str, then $character_mask. So you should have used: $post_Value = trim($str, "_");
Second, trim() strings the masked characters only from the beginning and end of the string. It doesn't remove any masked characters from the string if they are surrounded by non-masked characters.
You should actually use str_replace() with an empty replacement string (you've tried a single space as replacement):
$post_Value= str_replace("_", "", $key)
If you also want to remove <br> tags (in its typical variations), you may do so via single str_replace() call, as follows:
$post_Value= str_replace(array("_", "<br>", "<br/>", "<br />"), "", $key)
See str_replace() docs for details.
I found some
when I viewed the page resource. I don't have them in my code so this is confusing. I finally had to do a combination of str_replace() and rtrim() like this:
$post_Value= str_replace("<br_/>", "", $str); $post_Value2= str_replace("", " ", $post_Value); $post_Value3= rtrim($post_Value2,",submit,"); echo $post_Value3; //echo $editedStr=str_replace("", " ", $str); $query="UPDATE player_match SET categoryOption='$post_Value3' WHERE id=1";
rtrim() does not remove the string you specify in the second argument, but all characters in that string. In your case, that includes "h".
What you need here is a simple str_replace():
$folder = str_replace('.php', '', $file);
Edit: if you want to make sure it strips the ".php" part only from the end of the $file, you can go with @racetrack's suggestion below and use preg_replace() instead:
$folder = preg_replace('/\.php$/', '', $file);
rtrim's second argument is not the substring to remove but a set of characters—could also be a range. You can use preg_replace if you want to make sure only the trailing .php is removed. e.g.,
preg_replace("/\.php$/", "", "refinish.php")