-1

How to make a PHP regular expression that select the value of a specific parameter of the link, for example :

https://anylink.com/?s=a&start=19&info=abc The regular expression select the value of start parameter ( 19 in this case ) .

I tried with this pattern : [^start=]+$ but it only selects the the last parameter with = sign

Marox Tn
  • 142
  • 9
  • 2
    `[^…]` is a negated character class. What inspired you to that regex:? Btw, why not [`parse_url`](http://php.net/parse_url) / [`parse_str`](http://php.net/parse_str)? – mario Oct 25 '15 at 13:40
  • I think regular expression will work better for me because I want to replace the parameter, I tried to recreate this regular expression stackoverflow.com/questions/9703039/regex-for-picking-a-value-after – Marox Tn Oct 25 '15 at 14:03

2 Answers2

2

You can use parse_url() and parse_str() for that.

$parts = parse_url($url);
parse_str($parts['query'], $query);
echo $query['start'];
mario
  • 144,265
  • 20
  • 237
  • 291
Manikiran
  • 2,618
  • 1
  • 23
  • 39
0

This is probably what you are looking for:

<?php

$subject = 'https://anylink.com/?s=a&start=19&info=abc';
$pattern = '/start=([0-9]+)/';

preg_match($pattern, $subject, $tokens);

var_dump($tokens);
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Thanks, that's what I was trying to do – Marox Tn Oct 25 '15 at 14:10
  • I think you should change `[0-9]+` to `[^&]+`, because this way if the parameter gets changed from _integer_ to another type, it will still work. –  Oct 25 '15 at 14:46
  • @WashingtonGuedes That is certainly possible, but questionable. Typically a defined parameter does not suddenly change its type. In contrary, typcially one want's to make really certainly that one get's what one expects :-) So I would advise against it, but this certainly is a question of personal preferences and the situation itself which we do not know. – arkascha Oct 25 '15 at 14:47
  • I think it could turn into _decimal_ easily. But, it is only a suggest. –  Oct 25 '15 at 14:48
  • @WashingtonGuedes A parameter called `start` most likely refers to either a position in a queue, or an offset or an ID. In all cases an integer is what want to deal with. – arkascha Oct 25 '15 at 14:49