How does rtrim work? I have a string "4dbb3dca&". I am not sure how my string is formmated.
I want to call rtrim('4dbb3dca&', '&')
--edit:& might be $amp; special character issue
But after my string is 4dbb3dc.
I expect it to be 4dbb3dca.
Is there any workaround? I ried to use &, but then rtrim does nothing and initial string is returned. Is it possible to use rtrim in my situation? Does it depend on php version?
--- edit ---
after some research I realized that I want:
I want remove & using & in rtrim() function. Is this possible?
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&', '&')
It looks like you're trying to remove an ampersand from the end of the string, potentially in encoded form. rtrim doesn't give you enough control to do that, as you've seen, but you can do it with preg_replace.
echo preg_replace('/&(amp;)?$/', '', $string);
The pattern will match an ampersand at the end of the string, optionally followed by amp;.
The way the rtrim works remove the last characters at the second argument.
the PHP has split the string to characters and make a check if you have the characters at last of the string.
if you have two characters from the list of characters the rtrim will remove both
For example :
;&apm it will be the same result like &
if you want to remove the only ampersand symbol at the end of the string
use this code: rtrim('4dbb3dca&','&');
for understand more you can read about it at php.net
http://php.net/manual/en/function.rtrim.php
Related
Here's my string seperated by commas (,)
$myStr = "One, Two,Three, Four , Five ,Six "
I am able successfully explode it {assign var="options" value=","|explode:$myStr}
But I also would like to remove whitespace at the beginning or end from each string when outputting it. In other words: I am looking for a SMARTY function equivalent to PHP's built-in trim($mystr) function. PHP trim will remove the start and end white-spaces if it present, otherwise return the actual string.
{section name=myname loop=$options}
{$options[myname]}
{/section}
The code above would output:
One
Two
Three
[whitespace]Four[whitespace]
[whitespace]Five[whitespace]
Six[whitespace]
How can I trim whitespaces?
There is {strip}{/strip} in smarty.
Whitespaces will be removed between theese tags.
{$var|substr:0:-1} would remove last character in your case the whitespace. {$var|substr:1} would remove the first character from string.
all php-functions can be used as modifiers implicitly (more below) and
modifiers can be combined
from docs {$var|trim} in smarty is equal to trim($var) in php.
There is another way. Using {php}{/php} tags in in smarty to able to use php built-in trim() function, but that makes no sense to me. Why would you even want to put smarty variable back to php if it was included by php? Nonsense.
If the white space is just whitespace as in space characters, you can use the function trim() (Docs) to strip it. Replace $options[myname] with trim($options[myname]).
If it's the string "[whitespace]", you can use preg_replace() (Docs) like: preg_replace("/[whitespace]/","",$options[myname])
$string1 = "uploads/projects";
$string2 = "uploads/";
echo trim($string1, $string2); // outputs "rojects"
Can anyone tell me why trim() function is outputting rojects and not projects string?
According to the php manual of trim:
Optionally, the stripped characters can also be specified using the
character_mask parameter. Simply list all characters that you want
to be stripped. With .. you can specify a range of characters.
The second parameter behaves as a set of characters and not as a whole string.
So the character p which appears in your mask parameter uploads is being removed from the uploads/projects string.
Maybe you should use str_replace instead.
I need to trim the last 7 digits in one field of my form
and I need to trim the first three digits in another form
Im assuming that I use ltrim and rtrim but how exactly do I write it? I think i need to use ltrim and rtrim but I don't know how to write the code
ltrim() and rtrim() will remove the spacing characters.
Slapyo is correct, substr() is probably a way of doing it. It may look something like:
<?php
$Var = "mystringofcharacters";
$Result = substr($Var,0,strlen($Var)-7);
echo $Result;
?>
Should return: mystringofcha
We use strlen() because of how you want to return the string, "all characters except the last X" means you need to know how long it is, then get all the other characters.
If you wanted only the last 7 characters, you'd replace the strlen($Var)-7 section with just -7.
Here is what I am trying to achieve in PHP:
I have this string: host/%%%25asd%%
Now I want to loop through it and replace only the % _blank characters with %25. So I get the output as host/%25%25%25asd%25%25. (The %25 was untouched because the % wasn't followed by another %)
How should I go by doing this? regex? if so do you have an example? or loop through every character in the string and replace? I was thinking about using str_pos for this but it might after one replacement, the positions in the string would change :(
[Edit: Let me add a couple more information to ease up the confusion. %25 is just an example, it could be anything like %30 or %0a, I won't know before hand. Also the string could also be host/%%25asd%% so a simple replace for %% screw it up as host/%2525asd%25 instead of host/%25%25asd%25. What am trying to achieve is to parse a url into how google wants it for their websafe api. http://code.google.com/apis/safebrowsing/developers_guide_v2.html#Canonicalization. If you look at their weird examples.]
Use preg_replace:
$string = preg_replace('/%(?=%)/', '%25', $string);
Note the lookahead assertion. This matches every % that is followed by a % and replaces it with %25.
Result is:
host/%25%25%25asd%25%
EDIT Missed the case for the last %, see:
$string = preg_replace('/%(?=(%|$))/', '%25', $string);
So the lookahead assertion checks the next character for another % or the end of the line.
How about a simple string (non-regex) replace of '%%' by '%25%25'?
This is assuming you indeed want the output to be host/%25%25%25asd%25%25 as you mentioned and not one %25 at the end.
edit: This is another method that might work in your case:
Use the methods urlencode and urldecode, e.g.:
$string = urlencode(urldecode("host/%%%25asd%%"));
use str_replaceenter link description here instead of preg_replace , its a lot easier to apply and clean
How about something like this?
s/%(?![0-9a-f]+)/%25/ig;
$str = 'host/%%%25asd%%';
$str =~ s/ % (?![0-9a-f]+) /%25/xig;
print $str."\n";
host/%25%25%25asd%25%25
I'm looking for a way to replace a string in php that exactly matches with the subject.
For example I got a file named 'hello-world.txt' having three lines:
'http://www.example.com/'
'http://www.example.com/category/'
'http://www.example.com/tag/name/'
and I need to replace the 'http://www.example.com/' with 'http://www.example2.com'
$string=file_get_contents('hello-world.txt');
$string=str_replace('http://www.example.com/','http://www.example2.com',$string);
echo $string;
I will be getting a result similar to this:
'http://www.example2.com/'
'http://www.example2.com/category/'
'http://www.example2.com/tag/name/'
But What I actually need is something like this:
'http://www.example2.com/'
'http://www.example.com/category/'
'http://www.example.com/tag/name/'
Please Help!!!!
You can use preg_replace with the m modifier as:
$string=preg_replace('~^http://www\.example\.com/$~m','http://www.example2.com',$string);
Code In Action
First check if the current line is what you're looking for. If not, just spit it back out.
Either $string=str_replace("'http://www.example.com/'", "'http://www.example2.com'", $string); since in your example you have single quotes around each line or use preg_replace like this:
$string=preg_replace('/^http:\/\/www\.example\.com\/$/', 'http://www.example2.com/', $string);
... if those single quotes aren't supposed to be there. The $ at the end of the regex means the end of the line and the ^ means the beginning of the line. Periods and / need to be escaped hence the \. and \/
I haven't tested this code. Here is a link to preg_replace() http://php.net/manual/en/function.preg-replace.php