修剪字符串

发布于 2024-11-07 02:51:37 字数 386 浏览 3 评论 0原文

这可能很愚蠢,但我现在已经绞尽脑汁反对它了..

$values 包含以下文本:

'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest',

Why won't cut the last comma?

trim($values, ",");

编辑 - 这就是 $values 的生成方式:

$values = "";
foreach($users_table as $k=>$v){ $values .= "'$v',"; }
trim($values, ",");

This may be dumb but I've been beating my head against it for a bit now..

$values contains this text:

'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest',

Why won't this trim the last comma?

trim($values, ",");

EDIT - this is how $values is being generated:

$values = "";
foreach($users_table as $k=>$v){ $values .= "'$v',"; }
trim($values, ",");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软的没边 2024-11-14 02:51:37

字符串末尾后是否有可能有多余的空格?尝试打印它并用引号引起来。

Is it possible that there's extra whitespace after the end of your string? Trying printing it surrounded with quotes.

Hello爱情风 2024-11-14 02:51:37

最后一个逗号 , 之后可能有一些额外的字符。请参阅以下代码:

$str = "'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest', ";
var_dump(trim($str, " "));

OUTPUT

string(69) "'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest',"

尝试使用 var_dump 检查原始字符串,如下所示:

var_dump($str);

You probably have some extra character after last comma ,. See this code:

$str = "'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest', ";
var_dump(trim($str, " "));

OUTPUT

string(69) "'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest',"

Try checking your original string with var_dump like this:

var_dump($str);
若有似无的小暗淡 2024-11-14 02:51:37

我屈服了。

$values = preg_replace("/,$/", "", $values);

如果有人知道我做错了什么,我很乐意听到。这让我难住了。

I caved.

$values = preg_replace("/,$/", "", $values);

If anyone knows what I was doing wrong, I'd love to hear it though. It's got me stumped.

半窗疏影 2024-11-14 02:51:37

这对我有用,

<?php
$str = "'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest',";
echo( trim($str, ","));

?>

你也可以这样做

echo( trim(trim($str, " "),","));

,或者

echo(trim($str, ", "));

如果你只想将数组的值放入新数组中,只需使用 array_values()

$values = array_values($users_table);
echo($values);

this works for me

<?php
$str = "'58','val','val','val','2011-05-12 21-41-42','','0','micahstesttest',";
echo( trim($str, ","));

?>

you could also do this

echo( trim(trim($str, " "),","));

or

echo(trim($str, ", "));

if you just want to put the values of an array in to a new array just use array_values()

$values = array_values($users_table);
echo($values);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文