首页 > 解决方案 > 获取 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>";

标签: php

解决方案


你可以使用正则表达式。这将查找行首之后的第一个数字并将它们存储在捕获组 1 中:

$players = "Playing2Favorites25Visits2,206Created1/4/2019Updated4/5/2019Max Players20GenreRPGAllowed GearReport Abuse";

preg_match('/^[A-Z]+(\d+)/i', $players, $m);
echo $m[1];

输出:

2

推荐阅读