简体   繁体   English

php - 标记 href 属性中的正则表达式查询字符串

[英]php - regex query string in a tag href attribute

I would like to regex a query string which is contained in a href attribute of a tag.我想对包含在标签的 href 属性中的查询字符串进行正则表达式。 My aim is to get, for example, the page number of this a tag:例如,我的目标是获取此 a 标签的页码:

<a class="page-numbers" href="/website/admin/back.php?page=my_page&amp;pagenum=2&amp;id=12">next</a>

I would like to get the query pagenum .我想获得查询pagenum Is there a way to directly get it with a regex or someting like this?有没有办法用正则表达式或类似的东西直接获取它?

Here my code:这是我的代码:

$page_link = '<a class="page-numbers" href="/website/admin/back.php?page=my_page&amp;pagenum=3&amp;id=12">next</a>';

$page_nb = preg_match('/<a href="(.+)">/', $page_link, $matches);
$page_nb = (isset($matches[1])) ? $matches[1] : $page_nb;
$page_nb = parse_str(parse_url($page_nb, PHP_URL_QUERY), $params);
$page_nb = $params['pagenum'];

EDIT : I need this number because I will use it for a query.编辑:我需要这个号码,因为我将使用它进行查询。 The page number text not necessarily corresponds to the query page number.页码文本不一定对应于查询页码。 And the pagination can be a little bit complex.分页可能有点复杂。 I just need to retrieve this information for some stuff not relating on clicking on the button.我只需要为某些与单击按钮无关的内容检索此信息。

Where do you get that string from?你从哪里得到这个字符串? do you get it from a db?你是从数据库中得到的吗? api? api? is it hardcoded?它是硬编码的吗?

In your current code it looks like it's hardcoded, so why don't just do:在您当前的代码中,它看起来像是硬编码的,那么为什么不这样做:

$pagenum = 3;
$page_link = '<a class="page-numbers" href="/website/admin/back.php?page=my_page&amp;pagenum=". $pagenum . "&amp;id=12">next</a>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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