-5

I would like to regex a query string which is contained in a href attribute of a tag. My aim is to get, for example, the page number of this a tag:

<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. 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.

freaky
  • 990
  • 3
  • 17
  • 44
  • possible duplicate of http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element – Jay Blanchard Nov 17 '15 at 15:54
  • Sounds like the pagenum is equal to anchor text, so... – A. Wolff Nov 17 '15 at 15:54
  • Not necessarily in fact. It can be an integer or an html character because it's a pagination with next/prev buttons and dots... – freaky Nov 17 '15 at 15:55
  • @freaky Consider to ask question regarding your former issue and expected behaviour, not about any weirdo workaround. Why do you need it? How is it rendered? Your question is missing some context – A. Wolff Nov 17 '15 at 15:56
  • It's not about a work around. My aim is to find the easiest and best way to directly get the query string if it's possible. – freaky Nov 17 '15 at 15:57
  • @freaky On clicking on it or what? Again, your question seriously missing context – A. Wolff Nov 17 '15 at 15:58
  • It's in php there isn't any click. It's before the page is served to the browser. My question is how can I regex this string. – freaky Nov 17 '15 at 16:02
  • I don't understand why my question was put on hold. My question was clear: Is there a way to easily regex a query url in a tag href attribute – freaky Nov 17 '15 at 16:10

1 Answers1

0

Where do you get that string from? do you get it from a db? 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>';
Andy
  • 29,707
  • 9
  • 41
  • 58