【问题标题】:preg_replace all non-digit characters except the number between second and third slashpreg_replace 所有非数字字符,除了第二个和第三个斜杠之间的数字
【发布时间】:2017-08-05 19:09:20
【问题描述】:

假设输入字符串/en/614/5d/en/617/19,使用PHP 的正则表达式函数我想删除所有非数字(/\D/)字符,除了第二个和第三个斜杠之间的614617 . 我知道我可以使用 split 或 explode 来拆分字符串,但我需要一个正则表达式模式与 preg_replace 一起使用来仅提取第二个和第三个斜杠之间的数字。 谢谢。

【问题讨论】:

  • 你不想替换614和617的数字吗?
  • 如果你需要提取那个号码为什么不explode("/", $str)[2];
  • @bobblebubble 我不想要一个数组作为回报。
  • @marvel308 我不想替换问题中提到的第二个和第三个斜杠之间的任何内容。

标签: php regex preg-replace


【解决方案1】:
preg_replace('/^\/[^\/]+\/|\/.+$/','',$string);

【讨论】:

    【解决方案2】:

    我认为这应该更好更简单

    preg_replace('/\D/', '', $input_lines); //replace any non digit
    

    【讨论】:

    • 你应该(重新)阅读这个问题:“我想删除所有非数字 (/\D/) 字符,除了第二个和第三个斜杠之间的 614 或 617”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多