php - 获取 2 个单词之间的数字(输出中没有空格)
问题描述
我需要得到一个数字,它介于 2 个单词之间,即:
"Playing2Favorites25Visits2,206Created1/4/2019Updated4/5/2019Max Players20GenreRPGAllowed GearReport Abuse"
我想要第一个数字,即 2。
我用来提取该文本的代码是:
error_reporting(0);
@ini_set('display_errors', 0);
$link ="https://web.roblox.com/games/2710592004/Pictionary-Reborn";
//Get ROBLOX username
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$doc->loadHTMLFile($link);
$xpath = new DOMXPath($doc);
$query = "//div[@class='section-content remove-panel']";
$entries = $xpath->query($query);
$var = $entries->item(0)->textContent;
$players = $var;
echo "<p class='site'>$players</p>";
解决方案
你可以使用正则表达式。这将查找行首之后的第一个数字并将它们存储在捕获组 1 中:
$players = "Playing2Favorites25Visits2,206Created1/4/2019Updated4/5/2019Max Players20GenreRPGAllowed GearReport Abuse";
preg_match('/^[A-Z]+(\d+)/i', $players, $m);
echo $m[1];
输出:
2
推荐阅读
- javascript - 为什么即使我只导出一个函数,整个 javascript 文件也会执行
- swift - NSCollectionView 中的标题大小错误
- swift - 如何在一个 vc 中设置计时器并在另一个 vc 中触发它
- c - 通信独立的 C 模块
- tensorflow - 张量流模型动物园中的未知检查点文件
- javascript - 如何观察任何变量的变化(任何变量而不仅仅是对象)?
- c# - 即使将整个命令输出重定向到文件,如何在.NET 上输出到控制台(但不是标准错误)?
- nginx - Kubernetes 入口控制器 - 无法从外部虚拟机进行 TCP 连接,网络流量未转发到“服务”端口
- vue.js - Mixins 错误,使用 Vue Js 的 no-undef 问题
- javascript - 将未命名的 JSON 数组解析为 React 组件