Reputation: 580
My code is sometimes split over two lines like so:
I'm trying to match part of the URL by using the following regex:
https://www.url.com/(.*?)">Please Click Here
I have tried using /s
and /m
on the line but doesn't seem to match.
Any advice?
Upvotes: 1
Views: 245
Reputation: 70732
You don't need to use the s
(single line) or m
(multi-line) modifier here.
You could use something as simple as the following.
preg_match('~([^/]+)(?=">please\s+click\s+here)~i', $text, $match);
echo $match[1];
The i
modifier is used for case-insensitive matching.
See live demo
Upvotes: 1
Reputation: 44841
The problem is that "Please Click Here" won't match this:
Please Click
Here
The latter contains whitespace characters like \n
, \r
(maybe), and possibly \t
. Although it doesn't look like it contains \t
from the image you posted, it's better to try to handle that scenario, too. The \s
expression will catch a simple space (), as well as each of these characters.
Use this regex instead:
https://www\.url\.com/(?:[^"]*)(?=">Please\s+Click\s+Here)
Edit: tweaked further to return only the URL, not "Please Click Here" and the ">
.
Upvotes: 1
Reputation: 43683
For static url, basic search pattern would be:
/https:\/\/www\.url\.com\/([^\"]*)\"[^>]*>\s*Please\s+Click\s+Here/
But you better use $url
as a variable and include it in regex pattern as follow:
/<a\b[^>]*\burl=\"$url\"[^>]*>\s*Please\s+Click\s+Here\s*<\/a>/i
Upvotes: 1