With a post request, this is the result

/do/comment_all.php?url=/korabl_vikingov.html 

I need to "cut" from this link here is such a piece.

 /do/comment_all.php?url= 

I tried this way, it did not work out.

 $urlstr = $_POST['id']; $word = '\/do\/comment_all.php?url='; if(preg_match("/".$word."/i", $urlstr)) { $urlstr = preg_replace("\/do\/comment_all.php?url=", "", $urlstr); } 
  • If you know it in advance, why do you cut it? If this is a POST request, is the GET parameter present at all and is there another GET parameter in front of it? Is the URL exactly in $_POST['id'] ? - etki

1 answer 1

First, perhaps the string below

 /do/comment_all.php?url=/korabl_vikingov.html 

stored in the $ _SERVER ['REQUEST_URI'] variable.

Secondly, it is meaningless to check for a match using preg_match before replacing the same expression with the help of preg_replace , since if there is no match, there will be no replacement.

Thirdly, your regular schedule is wrong.
1) There is no indication of the beginning and end of a regular expression
2) Is the special character not escaped ?

As a result, your regular season (if you had not forgotten to designate its beginning and end) was looking for this:

 /do/comment_all.phurl= /do/comment_all.phpurl= 

The correct version of your regular expression is :

 @\/do\/comment_all\.php\?url= @i 

So, if you do not change your approach to the solution, and correct only obvious errors, then the solution is:

 $request = $_POST['id']; $pattern = '@\/do\/comment_all\.php\?url= @i '; $urlstr = preg_replace($pattern, "", $request); 

Or you can do it differently:

 $urlstr = $_POST['id']; $url_array = parse_url($urlstr); # Теперь в $url_array у нас следующее: # Array # ( # [path] => /do/comment_all.php # [query] => url=/korabl_vikingov.html # ) parse_str($url_array['query'], $query_array); # Теперь в $query_array у нас следующее: # Array # ( # [url] => /korabl_vikingov.html # ) # Если идет обращение к /do/comment_all.php и есть get-параметр url, то: if ($url_array['path'] === '/do/comment_all.php' && isset($query_array['url'])) { # Присвоим его значение переменной $urlstr = $query_array['url']; # Содержимое переменной: /korabl_vikingov.html }