Public methods of PHP development examples [detailed code explanation]

眼眸间的深情
Release: 2023-04-10 11:38:02
Original
1616 people have browsed it

When we step into the ranks of PHP development, we must always ask ourselves, keep learning, and keep summarizing. Only in this way can we go further and further on the road of PHP development. Today, Based on personal development examples, we have summarized some common common public methods to allow novice partners to carry out development practice activities faster during the development process:

1. Use the public method msubstr to intercept the Chinese string. If it is too long, use an ellipsis instead:

Usage scenarios:

Using this type of public method usually involves uploading some article data to the editor in the background, and the corresponding data needs to be displayed on the front end. Sometimes, When the background data is too long and the space displayed on the front end is not enough to display all the data, the redundant parts are replaced with ellipsis, which can make the front-end data display beautiful and simple, giving people a pleasing feeling.

Code display:

/** * 截取中文字符串,过长的使用省略号代替 */ function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){ $str = preg_replace("/]*>/i", "", $str); $str = preg_replace("/<\/a>/i", "", $str); $str = preg_replace("/]*>/i", "", $str); $str = preg_replace("/<\/div>/i", "", $str); $str = preg_replace("//i", "", $str);//注释内容 $str = preg_replace("/style=.+?['|\"]/i",'',$str);//去除样式 $str = preg_replace("/class=.+?['|\"]/i",'',$str);//去除样式 $str = preg_replace("/id=.+?['|\"]/i",'',$str);//去除样式 $str = preg_replace("/lang=.+?['|\"]/i",'',$str);//去除样式 $str = preg_replace("/width=.+?['|\"]/i",'',$str);//去除样式 $str = preg_replace("/height=.+?['|\"]/i",'',$str);//去除样式 $str = preg_replace("/border=.+?['|\"]/i",'',$str);//去除样式 $str = preg_replace("/face=.+?['|\"]/i",'',$str);//去除样式 $str = preg_replace("/face=.+?['|\"]/",'',$str);//去除样式只允许小写正则匹配没有带 i if(function_exists("mb_substr")){ $slice= mb_substr($str, $start, $length, $charset); }elseif(function_exists('iconv_substr')) { $slice= iconv_substr($str,$start,$length,$charset); }else{ preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); } $fix=''; if(strlen($slice) < strlen($str)){ $fix='...'; } return $suffix ? $slice.$fix : $slice; }
Copy after login

2.enctype encryption :

Usage scenarios:

Front-end password matching setting rules or re-encryption of back-end password matching rules to prevent other hackers from using common passwords The matching mechanism performs tasks such as website shutdown.

Code display:

/** * 公共方法 * 优化md5加密: */ function enctype($password) { return md5($password . C('MD5_SUFFIX')); }
Copy after login

Note:

C('MD5_SUFFIX') project is a constant for reading configuration "MD5_SUFFIX", the constant can be set by yourself.

3. Replace the middle 4 digits of the mobile phone number with *

Usage scenario:

After a user registers an account with a mobile phone number on the website, in order to protect the user's information security, replace the middle 4 digits of the mobile phone number with *, which will prevent the mobile phone number from being displayed completely, thus ensuring the user's information security to a certain extent.

Code display:

/** * 将手机号中间4位替换为* */ function suohao($phone){ $p = substr($phone,0,3)."****".substr($phone,7,4); return $p; }
Copy after login

4. Verify that the mobile phone number is correct:

Usage scenario:

Verify whether the mobile phone number filled in by the user is correct when the user registers the website, which facilitates the later maintenance of the data by our backend staff.

Code display:

/** * 验证手机号是否正确 * @author honfei * @param number $mobile */ function isMobile($mobile) { if (!is_numeric($mobile)) { return false; } return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false; }
Copy after login

5. Verify whether the input content is pure numbers:

Usage scenario:

Verification work when the user submits parameters that must be numeric items. After verification, corresponding feedback information can be given to the user to help the user submit data. effectiveness.

Code display:

/** * 验证输入的内容是否为纯数字 * @author wdy * @param number $mobile */ function isNumeric($number) { if (!is_numeric($number)) { return false; } return preg_match('/^\d+$/i', $number) ? true : false; }
Copy after login

6. Verify that the email is correct:

Usage scenarios:

When a user registers or binds email information, it is necessary to verify the true validity of the email, so that when users retrieve their password later, they can quickly and effectively receive the corresponding verification code.

Code display:

/** * 验证邮箱是否正确 * @author wdy * @param 18738536986@163.com $email */ function isEmail($email){ $mode = '/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/'; if(preg_match($mode,$email)){ return true; }else{ return false; } }
Copy after login

7. Recursive reordering of infinite classification arrays:

Usage scenarios:

Mall classification usually uses this method, which can effectively read and display the data of the mall classification, convenient for personal maintenance, and at the same time convenient for users. experience.

Code display:

//递归重新排序无限极分类数组 function recursive($array,$pid=0,$level=0){ //接收传递过来的数组 $arr = array(); foreach ($array as $value) { if($value['pid'] == $pid){ //定义分类级别 $value['level'] = $level; //定义分类分隔符号 $value['html'] = str_repeat('-', $level); //$arr[]来存储$value $arr[] = $value; //array_merge():函数把一个或多个数组合并为一个数组。 $arr = array_merge($arr,recursive($array,$value['id'],$level+1)); } } return $arr; }
Copy after login

8. Get the IDs of all category subcategories:

Usage scenarios:

Rapid reading of mall categories can quickly integrate and display classified information data, while facilitating users’ quick access experience.

Code display:

//获取所有分类子分类的ID function get_all_child($array, $id){ //定义一个数组 $arr = array(); //循环遍历 foreach ($array as $v) { //判断pid是否等于id if ($v['pid'] == $id) { //$arr接收所有的id $arr[] = $v['id']; //array_merge():函数把一个或多个数组合并为一个数组。 $arr = array_merge($arr, get_all_child($array, $v['id'])); } } return $arr; }
Copy after login

The above is the detailed content of Public methods of PHP development examples [detailed code explanation]. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!