【问题标题】:How can I remove a portion of a string from the right side of a string starting at a special character [duplicate]如何从以特殊字符开始的字符串右侧删除字符串的一部分[重复]
【发布时间】:2020-02-08 00:11:49
【问题描述】:

当页面刷新时,我创建了一系列作为 URL 的字符串。我想从这些字符串的右侧删除一部分。字符串看起来像:

http://funfun.ca/?image=1
http://funfun.ca/?image=14
http://funfun.ca/?image=217

等等。目标是任何以“?”开头的东西。从字符串的右侧删除并为所有字符串保留以下内容:

http://funfun.ca/

感谢任何帮助我解决这个问题

【问题讨论】:

  • 这是您的网站还是您将其作为字符串格式的 PHP 变量?
  • @DocZ 你看到我的回答了吗?

标签: php string operators truncate


【解决方案1】:

您可以explode? 的 url 字符串并仅保存获得的第一部分:

$firstPart = explode('?', $url)[0];

例子:

echo $firstPart = explode('?', 'http://funfun.ca/?image=1')[0];

返回:

http://funfun.ca/

【讨论】:

  • 感谢 user2342558!你让我今天一整天都感觉很好。谢谢一百万我的朋友
猜你喜欢
  • 2016-04-15
  • 1970-01-01
  • 2020-04-10
  • 2014-05-20
  • 1970-01-01
  • 2022-11-03
  • 2019-06-09
  • 2012-12-16
  • 1970-01-01
相关资源
最近更新 更多