【发布时间】:2015-06-04 09:48:37
【问题描述】:
让我们用 Windows 编辑器编写一个文件(因此,生成的 endline char 可能与 Unix 不同):
TITLE:Hello
URL:hello.html
AUTHOR:Bob
然后
content = file_get_contents($page);
preg_match("/^URL:(.*)$/m", $content, $matches);
echo $matches[1] . '#test';
返回
hello.html
#test
而不是
hello.html#test
我可以通过 View > Line Endings > Unix 解决问题并使用 Sublime Text 重新保存。
但是无论我使用哪种文本编辑器/平台,如何防止出现这个额外的空间?
【问题讨论】:
-
你可以在运行正则表达式之前使用
trim($content) -
@treegarden 哦,是的,没错,
trim($matches[1])解决了问题! (但不是trim($content)) -
哎呀。是的,当然你是对的。
-
@treegarden 如果你转换成答案,我会接受!
-
没关系。在我看来不值得回答;)谢谢
标签: php regex preg-match eol