【问题标题】:substring with last position in phpphp中最后一个位置的子字符串
【发布时间】:2012-11-29 05:45:03
【问题描述】:

我想要一个 php 中字符串的子字符串,其中包含起始和最后位置而不是长度。 因为我们在 php 中有函数 substr($str,$start_position,$length); 但我想要 $last_postion 而不是 $length 因为我不知道起始位置后的长度,因为它是可变的。 例如 $str = 2012 年 10 月 8 日 $str = 2012 年 2 月 2 日

【问题讨论】:

  • 如果你正在处理日期,那你就错了

标签: php string


【解决方案1】:

试试这个:

substr($str,$start_position,$last_postion-$start_position);

【讨论】:

  • @BhuvneshGupta - 如果您得到答案,请在其旁边添加绿色对勾。
  • @tbd 对不起,我不能投票,因为我的 15 声望
【解决方案2】:

好吧,如果您不知道确切的长度,则需要先使用 strpos。 http://php.net/manual/en/function.strpos.php

您使用 strpos($the_string, $the_string_to_find) 查找您要查找的字符,然后使用 $length 中的返回值作为 substr。

但正如@john 所说,如果您尝试操作日期或从日期字符串中获取一些值,使用 strtotime("october 8th, 2012") 会容易一百倍。然后,您可以根据需要格式化该日期,或使用第二个乘数对其进行加/减。

【讨论】:

    【解决方案3】:
    substr($str, $start_position, $last_postion-$start_position)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      相关资源
      最近更新 更多