我正在尝试使用trim从$_POST数组中返回的数据中删除下划线字符。我试过使用
$post_Value= str_replace("_", " ", $key)
但文本似乎并不是作为一个字符串返回的。它在每个入口之间都断了。然后我试着这样修剪:
$post_Value = trim("_", $str);
当我使用trim函数时,不会发生任何事情,它不会删除_
字符。我的最终目标是将逗号分隔的列表作为单个字符串放入数据库中。为什么我的trim()
函数在这种情况下不起作用?
首先,trim()
以相反的顺序接受参数:$str
,然后是$character_mask
。所以你应该使用:$post_Value = trim($str, "_");
其次,trim()
仅从字符串的开始和结束开始字符串掩码字符。如果字符串被非掩码字符包围,它不会从字符串中删除任何掩码字符。
实际上,您应该使用str_replace()
和一个空的替换字符串(您尝试过用一个空格作为替换):
$post_Value= str_replace("_", "", $key)
如果您还想删除<br>
标签(在其典型变体中),您可以通过单个str_replace()
调用来完成,如下所示:
$post_Value= str_replace(array("_", "<br>", "<br/>", "<br />"), "", $key)
有关详细信息,请参阅str_replace()
文档。
我在查看页面资源时发现了一些
。我的代码中没有它们,所以这很令人困惑。我最后不得不把str_replace()和rtrim()组合成这样:
$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";
尝试
preg_replace( '/^'W*(.*?)'W*$/', '$1', $string )
/* -----------------------------------------------------------------------------------
^ the beginning of the string
'W* non-word characters (all but a-z, A-Z, 0- 9, _) (0 or more times (matching the most amount possible))
( group and capture to '1:
.*? any character except 'n (0 or more times(matching the least amount possible))
) end of '1
'W* non-word characters (all but a-z, A-Z, 0-9, _) (0 or more times (matching the most amount possible))
$ before an optional 'n, and the end of the string
------------------------------------------------------------------------------------- */