This article mainly introduces the Nginxif statement and the regular expression to implement the string truncation function. This function may be required in special cases. one of NGINX's odd sex skills is, for more information, see nginx configuration. we used the set-misc-nginx-module.
location /test/ { default_type text/html; set_md5 $hash "secret"$remote_addr; echo $hash;}
The output content may be as follows:
202cb962ac59075b964b07152d234b70
But what if we want to intercept a certain number of characters?
First
For more XXXXXX articles, please follow the PHP Chinese website!
The idea must be implemented using modules, but is it only possible? Is there a more convenient way?
Yes.
We can skillfully use the if + regular expression to achieve this small requirement:
location /test/ { default_type text/html; set_md5 $hash "secret"$remote_addr; if ( $hash ~ ^[\w][\w][\w][\w][\w][\w][\w][\w]([\w][\w][\w][\w][\w][\w][\w][\w]) ) { set $hash $1; } echo $hash;}
To access/test/, the output is:
ac59075b
For more articles about string truncation using Nginx if statements and regular expressions, refer to PHP Chinese website!