Answer the question
In order to leave comments, you need to log in
When working with preg_replace, the element that I want to pull out of the line does not come out, what could be the problem?
I am writing my router for custom routing on the site. There was such a problem:
When running the following script, I do not get data from the string after applying the regular expression in preg_replace:
$pattern_regex = preg_replace("/\{(.*?)\}/", "(?P<$1>[\w-]+)", $pattern);
$pattern_regex = "#^" . $pattern_regex . "$#";
var_dump($pattern_regex);
/home
/user
/user/profile/{id}
/user/{id}/edit
string(9) "#^/home$#"
string(9) "#^/user$#"
string(32) "#^/user/profile/(?P<id>[\W-]+)$#"
string(29) "#^/user/(?P<id>[\W-]+)/edit$#"
string(9) "#^/home$#"
string(9) "#^/user$#"
string(32) "#^/user/profile/(?P[\W-]+)$#"
string(29) "#^/user/(?P[\W-]+)/edit$#"
Answer the question
In order to leave comments, you need to log in
Everything looks fine, the code is working and outputs what is expected
https://3v4l.org/X3JQA
I think the problem is that you see the results of the code in the browser, and they <id>
are processed as an html tag and therefore they are not visible.
In order to verify this, you can open the source code of the page (Ctrl + U) and see if there <id>
really is
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question