Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
How to preg_match all the digits and not the url coded commar

I the following URL https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343 After the https://test.example.com/blah/part of the URL, there are numbers being separated by %2C. I want to get an array of all the numbers in the URL. The number may a positive 1 all the way up to positive infinity. There may be an infinite amount of numbers in the URL or nothing at all (in this case the array is empty).

In this scenario, I want an array like: [12345, 80, 8263473, 9475834343]

$url = "https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343";

preg_match('/\b\d+\b/', $url, $matches);
print_r($matches);

However, the only element in my array is 12345

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You could also make use of the \G anchor to get those numbers:

(?:https?://\S+?/blah/|\G(?!^))(?:%2C)?\K\d+

Explanation

  • (?: Non capture group
    • https?://\S+?/blah/ Match the protocol and till the first occurrence of /blah/
    • | Or
    • \G(?!^) Assert the current position at the end of the previous match, not at the start
  • ) Close non capture group
  • (?:%2C)? Optionally match %2C
  • \K\d+ Forget what is matched so far, and match 1+ digits

Regex demo | PHP demo

Example

$re = '`(?:https?://\S+?/blah/|\G(?!^))(?:%2C)?\K\d+`m';
$str = 'https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343';

preg_match_all($re, $str, $matches);
print_r($matches[0]);

Output

Array
(
    [0] => 12345
    [1] => 80
    [2] => 8263473
    [3] => 9475834343
)
over 4 years ago · Santiago Trujillo Relatório

0

Consider just splitting on encoded characters, to generate an array of the numbers you want

$url = "https://test.example.com/blah/12345%2C80%2C8263473%2C9475834343";
$url = preg_replace("/^.*\//", "", $url);
$matches = preg_split("/%\w{2}/", $url);
print_r($matches);

This prints:

Array
(
    [0] => 12345
    [1] => 80
    [2] => 8263473
    [3] => 9475834343
)
over 4 years ago · Santiago Trujillo Relatório

0

Not sure how this would work with PhP in a working environment but would either of the following be an option to you?

%2C|(?!.*\/)(\d+)

Where you'd first match the "%2C" part and use an alternation to match groups of digits after your last forward slash to grab those groups of digits in an array. Again, I'd not be sure how you'd get an array from that 1st capture group.

If something like that would fail, maybe exclude the "%2C" part from your final array with backtracking control verbs (*SKIP)(*F) like so:

%2C(*SKIP)(*F)|(?!.*\/)\d+

A possible alternative way of writing this could be:

(?:^.*\/|%2C)(*SKIP)(*F)|\d+
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda