【问题标题】:Strip commas, whitespace, periods from string in php从 php 中的字符串中去除逗号、空格、句点
【发布时间】:2017-05-19 15:00:02
【问题描述】:

用户在两个单独的字段中提交姓名: 名字(author_fname)和姓氏(author_lname) 条目有时是 Smith、John J. 或 John and Mary Smith,或 john j。史密斯。 我需要从提交中删除所有无关的字符,所以名称只包含以下单词:smithjohnj 或 johnandmarysmith。 使用修剪,我能够删除空白。

但我也想去掉逗号。例如:

$text = $results["author_lname"].$results["author_fname"];
$trimmed = trim($text);
var_dump($trimmed);
$Aname = $trimmed;

【问题讨论】:

  • trim 只删除字符串开头和结尾的字符,而不是中间的字符。如果没有第二个参数,它只会删除空格。
  • 您应该尝试自己编写代码。在doing more research 之后,如果您有问题发布您尝试过的方法,并清楚地解释什么不起作用,并提供a Minimal, Complete, and Verifiable example。阅读How to Ask 一个好问题。请务必take the tour 并阅读this
  • 我就是这么想的。在查看手册时,我看到: string trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] ) 所以我的应该是: $text trim ( string $text [, string $character_mask = " ",.])

标签: php trim


【解决方案1】:

为此,您可以使用 php 方法:strtr,如下所述:http://php.net/manual/en/function.strtr.php

在您的情况下,您可以将 strtr 与已经修剪过的字符串一起使用,如下所示:

$stripped = strtr($trimmed, array(',' => ''));

这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 2016-08-17
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多