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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -