How do I pickout the final numeric from this array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I pickout the final numeric from this array?

Post by simonmlewis »

We are trying to get the final numeric values from this preg_match array.
https://www.site.co.uk/product/123/OLD- ... text-title
I'm not sure how to get it into a variable, so we can then query that valye, which in this scenario is 567.

Code: Select all

if (preg_match("^\/product\/([0-9]+)\/[^\/]\/([0-9]+)\/[^\/]\/([0-9]+)\/?$", $_SERVER['REQUEST_URI']) === 1) {
  print_r($matches);


  // Rewrite via DB connection here


  //  Array
  //  (
  //      [0] => 123,
  //    [1] => 3  45,
  //    [2] =  > 567
  //)


}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I pickout the final numeric from this array?

Post by Celauran »

Sounds like you want end. Alternately, check out array_pop.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I pickout the final numeric from this array?

Post by requinix »

Actually that array should have four elements, with [0] being the entire REQUEST_URI and 1-3 being what you've shown as 0-2 in your post.

The last number will always be $matches[3].
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I pickout the final numeric from this array?

Post by simonmlewis »

It would have, product/number1/word1/number2/word2/number3/word3
I want to get "number3" into a variable.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I pickout the final numeric from this array?

Post by requinix »

The code you posted does not work and the regex you're using does not match the input you have.

How about you fix those issues, then we can talk about you not knowing what capturing groups in regular expressions do.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I pickout the final numeric from this array?

Post by Christopher »

If you are using Apache (or nginx with pathinfo enabled) then you can just explode $_SERVER['PATH_INFO'] for the part of the URL that we commonly call the Route. Then it is just an array.

Code: Select all

$pathinfo = explode('/', $_SERVER['PATH_INFO']);
$numbers = [];
foreach ($pathinfo as $part) {
    if (is_numeric($part)) {
        $numbers[] = $part;
    }
}
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I pickout the final numeric from this array?

Post by simonmlewis »

the issue is, I need to first ensure that THAT is the style of the URL. But it's the bit on how to get that final number value that I am having difficulties with.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I pickout the final numeric from this array?

Post by simonmlewis »

simonmlewis wrote:We are trying to get the final numeric values from this preg_match array.
https://www.site.co.uk/product/123/OLD- ... text-title
I'm not sure how to get it into a variable, so we can then query that valye, which in this scenario is 567.

Code: Select all

if (preg_match("^\/product\/([0-9]+)\/[^\/]\/([0-9]+)\/[^\/]\/([0-9]+)\/?$", $_SERVER['REQUEST_URI']) === 1) {
  print_r($matches);


  // Rewrite via DB connection here


  //  Array
  //  (
  //      [0] => 123,
  //    [1] => 3  45,
  //    [2] =  > 567
  //)


}
Going back to the opening of this, if I use this I get the following error:

[text]Warning: preg_match(): Unknown modifier '\' in C:\xampp\phpMyAdmin\site-wide\module_301.php on line 6[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I pickout the final numeric from this array?

Post by Celauran »

This is what requinix was talking about. You don't have proper delimiters around your regex -- in fact, you don't have any -- and your proposed URI will not be matched by that regex. Take a look at PHP Live Regex to help sort this out. (Hint: what matches word3?)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I pickout the final numeric from this array?

Post by simonmlewis »

Funnily enough I have been looking at that website but cannot quite figure it out. I know virtually nothing about this stuff. If I put in all my string: ^\/product\/([0-9]+)\/[^\/]\/([0-9]+)\/[^\/]\/([0-9]+)\/?$, I dont know what to do next.

I Want to know if that URL format is being used. And if it is, to then capture the second to last entry (which is a prod id), and then query that in my database. Sounds simple, but I just cannot find the answer on how to do it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I pickout the final numeric from this array?

Post by Celauran »

OK, let's break it down bit by bit.

Here is your expression:

Code: Select all

^\/product\/([0-9]+)\/[^\/]\/([0-9]+)\/[^\/]\/([0-9]+)\/?$
http://php.net/manual/en/function.preg-match.php
First, your argument requires delimiters. Slap a / on either end and that's sorted.

Code: Select all

/^\/product\/([0-9]+)\/[^\/]\/([0-9]+)\/[^\/]\/([0-9]+)\/?$/
Now let's break this down into its constituent bits. (Note I've removed the escaping backslashes to improve readability)
^ starting at the beginning of the string
/product/ match the literal string '/product/'
([0-9]+) then match one or more digits and capture the result
[^/] match anything that is not a / once
([0-9]+) then match one or more digits and capture the result
[^/] match anything that is not a / once
([0-9]+) then match one or more digits and capture the result
/?$ end of string with an optional trailing slash.

[^/] is capturing a single character, but your URI will contain whole words, so there's a mismatch.
Your regex ends with a number and an optional trailing slash. Your URI ends with a word. Another mismatch.

Clearer?
Post Reply