Home >Backend Development >PHP Tutorial >PHP method to intercept a string between specified 2 characters, intercept the string_PHP tutorial
The example in this article describes the method of intercepting the string between specified 2 characters in php. Share it with everyone for your reference. The details are as follows:
In php, you only need to determine the stripos position before string 1 and string 2 and then use substr to start intercepting. Here is a simple example.
Usage:
$keyword='查找(计组实验)' $need=getNeedBetween($keyword, '(' , ')' );
After running the program:
$need='计组实验';
Let’s complete the string interception function getNeedBetween used above. This function can simply intercept the string between two specified characters ($mark1, $mark2) from the string ($kw). If it fails, it returns 0, and if it succeeds, it returns the intercepted string.
<?php function getNeedBetween($kw1,$mark1,$mark2){ $kw=$kw1; $kw='123′.$kw.'123′; $st =stripos($kw,$mark1); $ed =stripos($kw,$mark2); if(($st==false||$ed==false)||$st>=$ed) return 0; $kw=substr($kw,($st+1),($ed-$st-1)); return $kw; } ?>
I hope this article will be helpful to everyone’s PHP programming design.