Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - preg_match_all for backslash [] & [u002F]

I have a URL:

https:u002Fu002Fsite.vid.comu002F93836af7-f465-4d2c-9feb-9d8128827d85u002F6njx6dp3gi.m3u8?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb3VudHJ5IjoiSU4iLCJkZXZpY2VfaWQiOiI1NjYxZTY3Zi0yYWE3LTQ1MjUtOGYwYy01ODkwNGQyMjc3ZmYiLCJleHAiOjE2MTA3MjgzNjEsInBsYXRmb3JtIjoiV0VCIiwidXNlcl9pZCI6MH0.c3Xhi58DnxBhy-_I5yC2XMGSWU3UUkz5YgeVL1buHYc","

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 Reply

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);
print_r($matches[0]);
// => Array( [0] => https:u002Fu002Fsite.vid.comu002F93836af7-f465-4d2c-9feb-9d8128827d85u002F6njx6dp3gi.m3u8?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb3VudHJ5IjoiSU4iLCJkZXZpY2VfaWQiOiI1NjYxZTY3Zi0yYWE3LTQ1MjUtOGYwYy01ODkwNGQyMjc3ZmYiLCJleHAiOjE2MTA3MjgzNjEsInBsYXRmb3JtIjoiV0VCIiwidXNlcl9pZCI6MH0.c3Xhi58DnxBhy-_I5yC2XMGSWU3UUkz5YgeVL1buHYc )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...