PhpID prefix formatting class

Source: Internet
Author: User
Therefore, we should use int for storage, and perform some processing on the id during display, so that it is easy to tell which table the id belongs.


A database table usually has an auto-incrementing primary key named id, whose field type is int.

Advantage: The int type can be used for auto-increment, saving storage space than the character type.
Disadvantage: id is a numeric type. when you want to query the data corresponding to an id, it is difficult to determine which data table is based on the id.

Therefore, we should use int for storage, and perform some processing on the id during display, so that it is easy to tell which table the id belongs.


Write the following class to add a prefix to the id and restore the id with the prefix.

Prefix. class. php

 'U', self: ORDER_TYPE => 'O', self: MESSAGE_TYPE =>'m '); /*** create a prefix id * @ param Int $ prefix_type * @ return String */public static function getPrefixId ($ id, $ prefix_type = '') {// if (isset (self: $ prefix [$ prefix_type]) {return self: $ prefix [$ prefix_type]. $ id;} // no custom prefix type return $ id ;} /*** restore to id * @ param String $ prefix_id prefix id * @ return Int */public static fu Nction getId ($ prefix_id) {preg_match ('/\ d +/', $ prefix_id, $ arr); if (isset ($ arr [0]) {return $ arr [0];} return 0 ;} /*** obtain the prefix type based on the prefix id * @ param String $ prefix_id prefix id * @ return Int */public static function getPrefixType ($ prefix_id) {// obtain the id prefix preg_match ('/[A-Za-z] +/', $ prefix_id, $ arr); if (isset ($ arr [0]) {$ prefix = $ arr [0]; // Get the prefix $ prefixs = array_flip (self: $ prefix); if (isset ($ prefixs [$ pref Ix]) {return $ prefixs [$ prefix] ;}} return '';}}// class end?>

Demo. php

 '; // Prefix idecho' 1. id with prefix '. PHP_EOL; $ prefix_user_id = Prefix: getPrefixId ($ user_id, Prefix: USER_TYPE); $ prefix_order_id = Prefix: getPrefixId ($ order_id, Prefix: ORDER_TYPE ); $ prefix_message_id = Prefix: getPRefixId ($ message_id, Prefix: MESSAGE_TYPE); echo $ response; echo $ prefix_order_id.PHP_EOL; echo $ response; // Prefix type echo '2. obtain the prefix type based on the prefix id '. PHP_EOL; echo Prefix: getPrefixType ($ prefix_user_id ). PHP_EOL; echo Prefix: getPrefixType ($ prefix_order_id ). PHP_EOL; echo Prefix: getPrefixType ($ prefix_message_id ). PHP_EOL.PHP_EOL; // restore to original idecho 3. restore to original ID '. PHP_EOL; echo Prefix: getId ($ prefix_user_id ). PHP_EOL; echo Prefix: getId ($ prefix_order_id ). PHP_EOL; echo Prefix: getId ($ prefix_message_id ). PHP_EOL.PHP_EOL; echo'
';?>

Output:

1. add the id with the prefix U1001O2016102743765214M1092832. obtain the prefix userordermessage3. restore it to the original id10012016102743765214109283 based on the prefix id.

You can create custom prefix constants as needed.


Source code: Click to view

A database table usually has an auto-incrementing primary key named id, whose field type is int.

Advantage: The int type can be used for auto-increment, saving storage space than the character type.
Disadvantage: id is a numeric type. when you want to query the data corresponding to an id, it is difficult to determine which data table is based on the id.

Therefore, we should use int for storage, and perform some processing on the id during display, so that it is easy to tell which table the id belongs.


Write the following class to add a prefix to the id and restore the id with the prefix.

Prefix. class. php

 'U', self: ORDER_TYPE => 'O', self: MESSAGE_TYPE =>'m '); /*** create a prefix id * @ param Int $ prefix_type * @ return String */public static function getPrefixId ($ id, $ prefix_type = '') {// if (isset (self: $ prefix [$ prefix_type]) {return self: $ prefix [$ prefix_type]. $ id;} // no custom prefix type return $ id ;} /*** restore to id * @ param String $ prefix_id prefix id * @ return Int */public static fu Nction getId ($ prefix_id) {preg_match ('/\ d +/', $ prefix_id, $ arr); if (isset ($ arr [0]) {return $ arr [0];} return 0 ;} /*** obtain the prefix type based on the prefix id * @ param String $ prefix_id prefix id * @ return Int */public static function getPrefixType ($ prefix_id) {// obtain the id prefix preg_match ('/[A-Za-z] +/', $ prefix_id, $ arr); if (isset ($ arr [0]) {$ prefix = $ arr [0]; // Get the prefix $ prefixs = array_flip (self: $ prefix); if (isset ($ prefixs [$ pref Ix]) {return $ prefixs [$ prefix] ;}} return '';}}// class end?>

Demo. php

 '; // Prefix idecho' 1. id with prefix '. PHP_EOL; $ prefix_user_id = Prefix: getPrefixId ($ user_id, Prefix: USER_TYPE); $ prefix_order_id = Prefix: getPrefixId ($ order_id, Prefix: ORDER_TYPE ); $ prefix_message_id = Prefix: getPRefixId ($ message_id, Prefix: MESSAGE_TYPE); echo $ response; echo $ prefix_order_id.PHP_EOL; echo $ response; // Prefix type echo '2. obtain the prefix type based on the prefix id '. PHP_EOL; echo Prefix: getPrefixType ($ prefix_user_id ). PHP_EOL; echo Prefix: getPrefixType ($ prefix_order_id ). PHP_EOL; echo Prefix: getPrefixType ($ prefix_message_id ). PHP_EOL.PHP_EOL; // restore to original idecho 3. restore to original ID '. PHP_EOL; echo Prefix: getId ($ prefix_user_id ). PHP_EOL; echo Prefix: getId ($ prefix_order_id ). PHP_EOL; echo Prefix: getId ($ prefix_message_id ). PHP_EOL.PHP_EOL; echo'
';?>

Output:

1. add the id with the prefix U1001O2016102743765214M1092832. obtain the prefix userordermessage3. restore it to the original id10012016102743765214109283 based on the prefix id.

You can create custom prefix constants as needed.


The above is the content of the php ID prefix formatting class. For more information, see PHP Chinese website (www.php1.cn )!

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.