1
geeglo 2020-07-17 10:59:03 +08:00
trim 的常见误区,的第二参数不会作为整体去移除。而是每个字符单独左右移除
|
4
geeglo 2020-07-17 11:06:13 +08:00
没有,自己写。
|
5
weirdo 2020-07-17 11:06:56 +08:00 1
@nilai str_replace preg_replace
$url = str_replace('.git', '', $url) |
6
littleylv 2020-07-17 11:14:31 +08:00
手册有说明:
https://www.php.net/manual/en/function.trim.php#refsect1-function.trim-notes Note: Possible gotcha: removing middle characters Because trim() trims characters from the beginning and end of a string, it may be confusing when characters are (or are not) removed from the middle. trim('abc', 'bad') removes both 'a' and 'b' because it trims 'a' thus moving 'b' to the beginning to also be trimmed. So, this is why it "works" whereas trim('abc', 'b') seemingly does not. |
7
rming 2020-07-17 11:21:59 +08:00
$url = 'http://10.100.11.5/xapi.git';
$tmp = preg_replace("/\.git$/", "", $url); |
8
rming 2020-07-17 11:23:37 +08:00
preg_replace("/^start|end$/", "", "start content end")
|
11
nilai OP @rming 简单写了个函数 这样可好?
function strim($string,$removestring){ if (!is_string($string) || !is_string($removestring)){ return $string; } $result = preg_replace("/^{$removestring}|{$removestring}$/", "", $string); return $result; } |
12
nilai OP 改成这样可能会更好点。
function strim($string,$removestring=''){ if (!is_string($string)){ return $string; } if (!$removestring){ return trim($string); } $result = preg_replace("/^{$removestring}|{$removestring}$/", "", $string); return $result; } |
13
KasuganoSoras 2020-07-17 11:53:56 +08:00
@nilai #9 不要忘了 str_replace 有 count 参数
$url = str_replace(".git", "", $url, 1); 这样就只会替换一次了,另外如果你只是想单纯去掉结尾 .git 的话 $url = substr($url, -4, 4) == ".git" ? substr($url, 0, strlen($url) - 4) : $url; 完事了 |
14
airdge 2020-07-17 11:54:48 +08:00 2
这不是很正常
trim 会循环去除 charlist 里面的字符,直到完全移除 igithttp://10.100.11.5/xapiggtigigit , git 这个应该也只会输出 http://10.100.11.5/xap |
15
yc8332 2020-07-17 11:58:59 +08:00
这个是去除字符,不是字符串。。。你要去除字符串要用替换
|
16
WeKeey 2020-07-17 12:13:02 +08:00
substr pathinfo
|
17
jiehuangwei 2020-07-17 13:07:26 +08:00
有意思,明显没细看函数吧 猜猜这个输出 啥 trim('abc', 'bad')
|
18
sevenzhou1218 2020-07-17 13:29:40 +08:00
没仔细看手册,看了就不会问这个问题了
|
19
cbasil 2020-07-20 09:38:09 +08:00
去除字符串用 str_replace 或者 preg_replace 啊,trim 一般用来去掉字符串二端的空格
|