Home >Backend Development >PHP Tutorial >PHP uses substr() and strpos() to jointly find a specific character in a string, substrstrpos_PHP tutorial
The example in this article describes the use of substr() and strpos() in php A method to jointly find a specific character in a string. Share it with everyone for your reference. The specific analysis is as follows:
Requirement: Find the characters before the double vertical bar in the string below. Most of the clearing uses the explode() function, but too many explode()s were used during the project, so the following method was found.
<?php $str="admin||46cc468df60c961d8da2326337c7aa58||0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"; echo substr($str,0,strpos($str,"||")); ?>
The output content is admin.
Detailed description of substr(): http://www.bkjia.com/w3school/php/func_string_substr.htm
Detailed description of strpos(): http://www.bkjia.com/w3school/php/func_string_strpos.htm
I hope this article will be helpful to everyone’s PHP programming design.