Preg_replace is only part of the match.

I use preg_replaceto create urls for paging links based on modrewrite. I use:

$nextURL = preg_replace('%/([\d]+)/%','/'.($pageNumber+1).'/',$currentURL);

which works fine, however, I was wondering if there is a better way without having to include '/'replacements in the parameter. I need to match the number as between two /, since URLs can sometimes contain numbers other than part of the page. However, these numbers are not only numbers, therefore, /[\d]+/does not allow them to be replaced.

+3
source share
2 answers

You can use the search rules :

 %(?<=/)([\d]+)(?=/)%

(?<=…) - , (?=…) - . (?<=/)([\d]+)(?=/) :

  • (?<=/) - /
  • ([\d]+) -
  • (?=/) - /

:

preg_replace('%(?<=/)\d+(?=/)%', $pageNumber+1, $currentURL)
+8

Try

$nextURL = preg_replace('%(?<=/)([\d]+)(?=/)%',($pageNumber+1),$currentURL);
+2

Source: https://habr.com/ru/post/1742094/


All Articles