2

I need to extract an item id from a URL; the pattern is this &R=10031004&, I mean, I need to extract the string within &R= and the other &

This is what I have so far, but I keep getting errors.

preg_match('^[&R=]+ &$',
"http://www.lapdirecciondemario.com/items.php&R=10031004&", $matches);
$host = $matches[0];

echo $host;
1
  • Your pattern is missing the delimiters - it's an invalid pattern. Commented Oct 24, 2011 at 18:09

7 Answers 7

2
$url = 'http://www.lapdirecciondemario.com/items.php&R=10031004&';
preg_match('/(&|\?)R=([0-9]+)/i', $url, $matches);
echo $matches[2];

In contrast to the other answers, this will also match if the parameter R is preceded by a ? instead of a &. If you know more about the length of the number, you can replace the + after [0-9] with {min,max}.

This regular expression should be pretty robust and match all of these:

Sign up to request clarification or add additional context in comments.

Comments

1

Use this:

preg_match('/&R\=(.*?)&/i', "http://www.lapdirecciondemario.com/items.php&R=10031004&", $matches);
echo $matches[1]; // will echo 10031004

2 Comments

@middus I answered the OP's question that ask to get the string between &R= and &, so there was no need to do a regex for other strings.
Yeah, I know. But the URL given by the OP is likely to be invalid.
0
preg_match('#R=([0-9]+)#is', "http://www.lapdirecciondemario.com/items.php&R=10031004&", $matches);
echo $matches[1]; # 10031004

1 Comment

@middus, it can be pretty much anything as long as Delimiter must not be alphanumeric or backslash is satisfied. Try ~ or even * for example.
0

well first of all maybe I can simplify your task,

is this the url from the script?

then you should check the contents of $_GET where you will find a variable R with contents 10031004...

$item_id = $_GET['R']; 
echo $item_id; 

1 Comment

I think he is scraping website :)
0
if (preg_match('/(?<=&R=).*?(?=&)/', "http://www.lapdirecciondemario.com/items.php&R=10031004&", $regs)) {
    $result = $regs[0];
}

Use this one.

2 Comments

why making simple things look complicated? :)
:) Why capture something not needed? :)
0

I might as well chuck another at you. This one will match any character between an R= preceeded by ? or & and ending with & or #.

preg_match('/[?&]R=([^&#]+)/', "http://www.lapdirecciondemario.com/items.php&R=10031004&", $matches);

5 Comments

If the id is supposed to be numeric, this does not add value.
Yes, but his question says that he has to 'extract the string within &R= and the other &', so my answer is more precise.
Your answer would match http://www.lapdirecciondemario.com/items.php&R=1003/IamtheWalrus/1004&, which is probably not what he wants or is it?
Your regex returns 1003. Mine returns '1003/IamtheWalrus/1004'. Mine is more correct, based on the wording of the question. On the other hand, that input is absurd and neither is likely to be useful to lapdirecciondemario.com's meaning of R.
There is no "more correct", just correct and not correct. If we assume the id to be numeric, this URL should not match. If we knew more about the id, we could specify {min,max} (see my answer) to further avoid false positives.
0
$url = "http://www.lapdirecciondemario.com/items.php&R=10031004&";
$urlQueryParts = array();
parse_str(parse_url($url, PHP_URL_QUERY), $urlQueryParts);
if(isset($urlQueryParts[ "R" ]))
    $urlQueryParts[ "R" ];

I couldn't test it but it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.