Basj
Basj

Reputation: 46183

preg_match adds an unwanted whitespace

Let's write a file with a Windows editor (thus, generating different endline char than 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 the problem by doing View > Line Endings > Unix and resave with Sublime Text.

But how to prevent this additional space to appear, regardless which text editor / platform I use?

Upvotes: 0

Views: 71

Answers (2)

Toto
Toto

Reputation: 91488

You could do:

preg_match("/^URL:(\S+)/", $content, $matches); 

\S+ matches at least one character that is not a space character. URL doesn't contain spaces, so in group 1 you have the url without spaces at the end.

If the string you want to match have spaces in the middle:

preg_match("/^URL:(.+?)\s*$/", $content, $matches); 

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89574

Several possibilities:

1) you can use a non-greedy quantifier followed by an optional carriage return:

/^URL:(.*?)\r?$/m

2) you can describe more explicitly the capture group in a way that it doesn't end with a whitespace character:

/^URL:\h*(\S+(?:\h+\S+)*)/m

\h is a character class for horizontal whitespaces.

Upvotes: 0

Related Questions