-2

Im actively practicing with php regex and got stuck with a replace function as it shows absolutely different results from tutorial I copied it from.

So the function preg_replace('/\{([a-z]+)\}/', '(?P<\1>[a-z-]+)', '{controller}\/{action}') should give me this result in the output: (?P<controller>[a-z-]+)\/(?P<action>[a-z-]+). phpliveregex.com confirms that https://www.phpliveregex.com/p/wIF

However, this is what my browser on my localhost is outputing me in fact: (?P[a-z-]+)\/(?P[a-z-]+). How is that possible? Is my php broken or the preg_replace function is working differentely now? My php version is 7.4.4

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

0

Your browser interprets the <controller> and <action> as html tags and tries to parse them that leads to deleting them. see this to see how can you show tags literally using html encoding.

(?P<controller>[a-z-]+)\/(?P<action>[a-z-]+)

<br/>

(?P&lt;controller&gt;[a-z-]+)\/(?P&lt;action&gt;[a-z-]+)

You can maybe replace it with (?P&lt;\1&gt;[a-z-]+)

mjrezaee
  • 1,100
  • 5
  • 9