需要一个特定的正则表达式才能从这个特定的链接结构中获取数据


Need a specific regex to get the data from this specific link structure

我需要使用正则表达式获取 url 或数字(这个数字只是一个例子,但所有数字都有 7 位数字)?必须不要匹配 HTML 文件中所有可用的<a>,而是使用此确切的结构。

<a href="./view/3049532/">

这甚至不难。

/<a's+href="'.'/view'/('d{7})'/">/

查看演示

<?php
$page_content = <<<THIS
<a href="./view/3049532/">
<a href="./view/398562/">
<a href="./view/3652872/">
<a href="./view/3785471/">
THIS;
    preg_match_all('/<a's+href="'.'/view'/('d{7})'/">/', $page_content, $matches);
    print_r($matches[1]);

输出:

Array
(
    [0] => 3049532
    [1] => 3652872
    [2] => 3785471
)

而且根据其他人的话,如果这是您的最后选择,只需使用正则表达式!