简体   繁体   English

PHP中TRIM的不同行为

[英]Different Behaviour of TRIM in php

I have a piece of code, from this i want to remove some words from string but the trim() function is not showing proper output. 我有一段代码,从这里我想从字符串中删除一些单词,但是trim()函数未显示正确的输出。

The code is below 代码如下

<?php
$mystring = '/word-quotes-hope';
$findme   = '/word-quotes-';
echo $str = trim($mystring, $findme);
?>

and its output is hop , but it should be hope 它的输出是hop ,但应该是hope

Its working example http://codepad.viper-7.com/FxLZkp 其工作示例http://codepad.viper-7.com/FxLZkp

Anybody knows why this is happening. 任何人都知道为什么会这样。

因为trim使用第二个字符串而不是整个字符串,而是单独的字符,并且在您的情况下,其中包含e ,并且字符串以e结尾,所以它消失了。

Try to use it with "ltrim" 尝试将其与“ ltrim”一起使用

echo $str = ltrim($mystring, $findme);

it will trims your string on "left" 它将在“左侧”修剪您的字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM