NeverPhased
NeverPhased

Reputation: 1566

How to include new line "\n" in PHP regex match

I have a regex as follows:

$regex = '/(https?\:\/\/|www)[^(\"|\\\) ]+/i';

Which I want to extract just the url from the following:

Here is some dummy text http://testwebsite.com.eu\n\nIf you don't like this dummy text then tough luck.

My result returns

http://testwebsite.com.eu

If

How can I change my regex so it includes the \n in its search?

Edit - a link to my regex101 query works. But when trying to implement in php it doesn't. Please note I am running the php through CLI. https://regex101.com/r/lNovMJ/1

Upvotes: 3

Views: 2308

Answers (2)

user1658543
user1658543

Reputation:

The regular expression

/\r\n?|\n/

matches all three types of newlines, namely \n on Linux, \r\n on Windows and \r on Mac.

Upvotes: 0

anubhava
anubhava

Reputation: 786359

You can use this regex to make it work:

~\b(?:https?://|www)[^"\\\s]+~

\s inside negated character class will stop matching when we encounter any whitespace (including newlines).

RegEx Demo

Upvotes: 4

Related Questions