Help create a regular expression. I have the regular expression '~^/example/(\d+)$~' , it matches any string starting with "/ example /" and ending with a number. I need the same thing, only so that instead of the last slash there is a grid. I thought it would work like '~^/example#(\d+)$~' , but no, it doesn't plow.

UPD:

  1. The regular expression successfully processes such a string / example / 2
  2. The regular expression should find all the lines of such a plan / example # 2, / example # 3, / example # 425
  3. I tried instead of a slash to put \ x23, did not help

Perhaps the fact is that in the code they are processed by me like this, this is a single entry point on the site

  $this->controllerFound = false; foreach ($this->routes as $key=>$value){ if(preg_match("$key", $this->request_URI,$matches)){ include_once ROOT.'/Controllers/'.$value['controller'].'.php'; $Controller = new $value['controller'](); array_shift($matches); $this->controllerFound = call_user_func_array(array($Controller, $value['action']), $matches); if ( $this->controllerFound == true) { break; } } } 

$this->routes is an array

 $routes= array( '~^/$~' => array( 'controller' => 'MainPagesController', 'action' => 'homePage' ), '~^/list/(\w+)$~' => array( 'controller' => 'MainPagesController', 'action' => 'coursesListPage' ), '~^/course/(\d+)$~' => array( 'controller' => 'MainPagesController', 'action' => 'coursePage' ), '~^/example#(\d+)$~' => array( 'controller' => 'MainPagesController', 'action' => 'commentsPage' ), '~^/comments_jq~' => array( 'controller' => 'MainPagesController', 'action' => 'comments_jq' ) ); 
  • Use ascii-code - # = \x23 - Andrey Fedorov
  • Give an example of the line, please. ^/example#(\d+)$ will find a match in /example#123 . - Wiktor Stribiżew 5:33 pm
  • You can test here regex101.com - Andrey Fedorov
  • one
    @AndreyFedorov separator mark here ~ - Wiktor Stribiżew pm
  • one
    Are you sure that you have this grid and symbols after it at the entrance to the script? I understand this is the url that you get from the client. But the browser in any case will pass you a line without # and everything after it, since this is a label that is processed only on the client and according to HTTP standards, it should not be transmitted to the server - Mike

1 answer 1

No Ie directly in any way. It will not be reflected there, because it is not transmitted to the server. For example, some web applications protect keys to api. The easiest option is to insert a javascript that will copy the anchor (something that after # used to be an anchor) in a cookie or send by an Ajax in a separate request.