Page 1 of 1

How do I pickout the final numeric from this array?

Posted: Fri Sep 16, 2016 11:26 am
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
  //)


}

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

Posted: Fri Sep 16, 2016 11:33 am
by Celauran
Sounds like you want end. Alternately, check out array_pop.

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

Posted: Fri Sep 16, 2016 3:09 pm
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].

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

Posted: Fri Sep 16, 2016 3:32 pm
by simonmlewis
It would have, product/number1/word1/number2/word2/number3/word3
I want to get "number3" into a variable.

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

Posted: Sat Sep 17, 2016 1:21 am
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.

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

Posted: Sat Sep 17, 2016 11:44 am
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;
    }
}

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

Posted: Mon Sep 19, 2016 8:09 am
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.

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

Posted: Mon Sep 19, 2016 12:06 pm
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]

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

Posted: Mon Sep 19, 2016 12:53 pm
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?)

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

Posted: Mon Sep 19, 2016 1:03 pm
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.

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

Posted: Mon Sep 19, 2016 1:30 pm
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?