【发布时间】:2015-01-13 14:41:51
【问题描述】:
我正在尝试编写一个函数,该函数将在损坏的 URL 中插入一个反斜杠,由于某种原因,该 URL 在 http 之后缺少第二个反斜杠,我只想返回固定版本,例如
addSlash(http:/example.net) -> http://example.net
addSlash(https:/example.net`) -> https://example.net
我认为这可以通过 preg_replace 在一行代码中解决,但我无法让它工作。使用 $url = 'http:/example.net' 和
preg_replace("@^(https?:)(.*?)@", "\1/\2", $url);
我回来/ /example.net,好像'http'不匹配并放入\1。
有什么建议吗?如果可能的话,我想避免回调和匿名函数,因为这应该在旧版本的 PHP 上运行。
【问题讨论】:
标签: php preg-replace