PHP:文本爆炸()问题
我对explode() 函数有疑问。我使用该函数来分解诸如“Name:Replica”之类的字符串,但有时字符串中有2个或更多冒号(“:”),并且存在问题,因为我的脚本是: 示例:“名称:replica:replica2:replica3”
$explode = explode(":", $string);
$query = "INSERT INTO `table` (`field_1`, `field_2`) VALUES ('".$explode[0]."', '".$explode[1]."')";
我需要解决此问题。因为当我在第一个冒号(“:”)之后分割字符串时,第二部分必须是最后一部分。
问候, 乔治!
PS-对不起我的英语。
I have a problem with explode() function. I use the function to explode strings like "Name: Replica" but sometimes in the string has 2 or more colons ( ":" ) and there is the problem because my script is:
Example: "Name: replica:replica2:replica3"
$explode = explode(":", $string);
$query = "INSERT INTO `table` (`field_1`, `field_2`) VALUES ('".$explode[0]."', '".$explode[1]."')";
And I need any solution for this problem. Because when I split the string after first colon ( ":" ) the second part must be the last part.
Regards,
George!
P.s. - Sorry for my English.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您想使用
explode()
的“limit”(第三个)参数:这将确保您只得到两个结果。
http://php.net/manual/en/function.explode.php
I think you want to use the 'limit' (third) argument to
explode()
:That will make sure you only get two results.
http://php.net/manual/en/function.explode.php
对
explode()
使用可选的第三个$limit
参数:这告诉
explode()
返回最多包含 2 个元素的数组,将所有元素随后的冒号插入返回的第二个字符串片段中。请注意,根据您的示例,您应该使用冒号加空格:但也许这只是一个巧合。
Use the optional third
$limit
parameter toexplode()
:This tells
explode()
to return an array with at most 2 elements, putting all subsequent colons into the second string fragment returned. Note, according to your examples you should be using a colon plus a space:But maybe that's just a coincidence.
按照@Jon Nalley 的建议进行编辑。请注意,limit(第三个参数)仅受 PHP 5.x 支持
edited as suggested by @Jon Nalley. Note that limit (3rd parameter) is only supported by PHP 5.x