How to intercept a string between specified 2 characters in php

高洛峰
Release: 2023-03-04 15:14:01
Original
2911 people have browsed it

The example in this article describes how PHP intercepts a string between two specified characters. 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.

<?php
function getNeedBetween($kw1,$mark1,$mark2){
$kw=$kw1;
$kw=&#39;123′.$kw.&#39;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;
}
?>
Copy after login


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

For more related articles on how ob_start and other functions in PHP intercept standard output, please pay attention to the PHP Chinese website!

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