Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

php - preg_match_all for backslash [] & [u002F]

I have a URL:


And I want to match it using preg_match_all. My regex expression is:

preg_match_all('/(https://site.vid.com/.*",")/', $input_lines, $output_array);

But I am not able to match special character & u002F in above code. I tried using (escaping fuction). But it is not matching. I know it maybe a lame question, but if anyone could help me in matching and u002F or in escaping and u002F in preg_match_all, that would be helpfull.

Question Edit:

I want to use only preg_match_all because I am trying to extract above URL from a html page.

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may use

preg_match_all('~https:(?://|(?:\\u002F){2})site.vid.com(?:/|\\u002F)[^"]*~', $string)

See the regex demo. Details:

  • https: - a literal string (if s is optional, use https?:)
  • (?://|(?:\u002F){2}) - a non-capturing group matching either // or (|) two occurrences of u002F
  • site.vid.com - a literal site.vid.com string (the dot is a metacharacter that matches any char but line break chars, so it must be escaped)
  • (?:/|\u002F) - a non-capturing group matching / or u002F text
  • [^"]* - a negated character class matching zero or more chars other than ".

See the PHP demo:

$re = '~https:(?://|(?:\\u002F){2})site.vid.com(?:/|\\u002F)[^"]*~';
$str = 'https:\u002F\u002Fsite.vid.com\u002F93836af7-f465-4d2c-9feb-9d8128827d85\u002F6njx6dp3gi.m3u8?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb3VudHJ5IjoiSU4iLCJkZXZpY2VfaWQiOiI1NjYxZTY3Zi0yYWE3LTQ1MjUtOGYwYy01ODkwNGQyMjc3ZmYiLCJleHAiOjE2MTA3MjgzNjEsInBsYXRmb3JtIjoiV0VCIiwidXNlcl9pZCI6MH0.c3Xhi58DnxBhy-_I5yC2XMGSWU3UUkz5YgeVL1buHYc","';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// => Array( [0] => https:u002Fu002Fsite.vid.comu002F93836af7-f465-4d2c-9feb-9d8128827d85u002F6njx6dp3gi.m3u8?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb3VudHJ5IjoiSU4iLCJkZXZpY2VfaWQiOiI1NjYxZTY3Zi0yYWE3LTQ1MjUtOGYwYy01ODkwNGQyMjc3ZmYiLCJleHAiOjE2MTA3MjgzNjEsInBsYXRmb3JtIjoiV0VCIiwidXNlcl9pZCI6MH0.c3Xhi58DnxBhy-_I5yC2XMGSWU3UUkz5YgeVL1buHYc )

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share