简体   繁体   中英

Extracting JS from html with preg_match

我只需要从html 中提取client_token值。这是我迄今为止的尝试

preg_match("#var client_token(.*)=(.*)'(.*?)'#is",  $html, $match);

使用 \\s+ 而不是 .* 以避免匹配随机内容:

preg_match("#var client_token\s*=\s*'(.*?)'#is",  $html, $match);

If the html code is constant you can use bottom code.

preg_match("/var client_token = '(.*?)';/",  $html, $match);

But if space and qoute is unknown use this

preg_match("/var\s+client_token\s*=\s*[\'|\"](.*?)[\'|\"]\s*;/",  $html, $match);
  1. \\s+ match one/multiple space
  2. \\s* match none/one/multiple space
  3. ['|"] match ' or "

This is the most compact and easy to understand:

preg_match("/client_token[^\']*\'([^\']*)/",  $html, $match);

Tested and explained here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM