Use a regular expression in the Nginxif statement to perform string truncation.

Source: Internet
Author: User
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!




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.