Home > Backend Development > PHP Tutorial > PHP method to intercept a string between specified 2 characters, intercept the string_PHP tutorial

PHP method to intercept a string between specified 2 characters, intercept the string_PHP tutorial

WBOY
Release: 2016-07-13 09:57:04
Original
994 people have browsed it

How to intercept the string between specified 2 characters in php, intercept the string

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, '(' , ')' );
Copy after login

After running the program:

$need='计组实验';
Copy after login

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.

<&#63;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;
}
&#63;>
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/984619.htmlTechArticleHow to intercept the string between specified 2 characters in php, intercept the string. The example in this article tells about php intercepting the specified 2 characters. String method between characters. Share it with everyone for your reference. Specifically...
Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template