In PHP the trim function has a parameter for trimming specific characters (handy for leading zeros and the like). I can't seem to get it to accept a vertical bar (|) character. Anyone know how to get this working? I tried the hex value but had no luck. I'm sure it's something simple.
Cheers
It works for me:
var_dump(trim('|foo|', '|')); // string 'foo' (length=3)
Maybe you have some whitespace around it, or your're using the wrong pipe character? ¦ vs |
Works for me:
$str = "|test string";
echo trim($str, "|");
test string
Can you show some of your code?
Maybe you want to remove a | in the middle of a string
you can use str_replace
str_replace("|", "", $str);
echo trim('|text|', '|'); // returns text
The second param was added in PHP 4.1!
trim() only removes characters from the beginning and end of a string. If you'd like to replace characters in the middle of a string, use str_replace(), or preg_replace() if you like regular expressions.
Related
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
I'm trying to remove part of string in my php script. The strings will be similar to this one:
Samsung I8730 Galaxy Express
I need to remove part "I8730", this will be used on other models like "i9500", "B2100", etc. etc.
Please assist with some preg_replace pattern or something that will fix this problem.
Thanks.
A letter is [A-Za-z]. A number is \d.
/[A-Za-z]\d{3,4}/
First explode the string using on " ".
And then use preg_match() for the following regex.
^[a-zA-Z][0-9]{4}$
Use a regex and replace by nothing, this one matches if string has at least 1 char and 1 number:
$string = preg_replace('/[A-Za-z]+[0-9]+/', '', $string);
assume that after character there can be 2-5 intergers
$str = 'Samsung I8730 Galaxy Express';
echo preg_replace( '/([a-zA-Z][0-9]{2,5} )/','', $str);
This might helpful
I have a string $test='23487°';
How can I remove all the instances of the little circle that appears in the string using preg replace?
What to I enter for the regex to remove it?
EDIT - as Pekka says, str_replace is better I am now using that. But the little circle is still not recognized by PHP...
You don't need regex, just str_replace:
$test = str_replace('°', '', $test);
The first parameter is the search term – the bit that will be found. The second parameter is the replacement string – the text that will be inserted instead. A blank string means "replace it with nothing", i.e. "remove it". The third parameter is the string on which to operate.
try with:
$test = preg_replace('/[^(\x20-\x7F)]*/','', $test);
this will replace all your non ascii characters from your string.
If you want to use preg_replace, you can do it like this:
$test = preg_replace('[°]', '', $test);
Also, for reference, here is a great site to test your regex:
http://www.solmetra.com/scripts/regex/index.php
I am pretty new to regular expressions.
I need to clean up a search string from spaces at the beginning and the end.
Example: " search string "
Result: "search string"
I have a pattern that works as a javascript solution but I cant get it to work on PHP using preg_replace:
Javascript patern that works:
/^[\s]*(.*?)[\s]*$/ig
My example:
$string = preg_replace( '/^[\s]*(.*?)[\s]*$/si', '', " search string " );
print $string; //returns nothing
On parse it tells me that g is not recognized so I had to remove it and change the ig to si.
If it's only white-space, why not just use trim()?
Yep, you should use trim() I guess.. But if you really want that regex, here it is:
((?=^)(\s*))|((\s*)(?>$))
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