php - preg_match adds an unwanted whitespace -
let's write file windows editor (thus, generating different endline char unix probably):
title:hello url:hello.html author:bob
then
content = file_get_contents($page); preg_match("/^url:(.*)$/m", $content, $matches); echo $matches[1] . '#test';
returns
hello.html #test
instead of
hello.html#test
i can solve problem doing view > line endings > unix
, resave sublime text.
but how prevent additional space appear, regardless text editor / platform use?
you do:
preg_match("/^url:(\s+)/", $content, $matches);
\s+
matches @ least 1 character not space character. url doesn't contain spaces, in group 1 have url without spaces @ end.
if string want match have spaces in middle:
preg_match("/^url:(.+?)\s*$/", $content, $matches);
Comments
Post a Comment