Home > php教程 > php手册 > body text

php截取指定2个字符之间字符串的方法,截取字符串

WBOY
Release: 2016-06-13 09:06:56
Original
1260 people have browsed it

php截取指定2个字符之间字符串的方法,截取字符串

本文实例讲述了php截取指定2个字符之间字符串的方法。分享给大家供大家参考。具体如下:

在php中只要判断字符串1与字符串2之前的一个stripos位置然后再使用substr开始截取就可以了,这里给大家介绍一个简单例子。

使用方法:

$keyword='查找(计组实验)'
$need=getNeedBetween($keyword, '(' , ')' );
Copy after login

运行该程序之后:

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

下面就来完成上面用到的字符串截取函数getNeedBetween。该函数可实现简单的从字符串($kw)截取两个指定的字符($mark1,$mark2)之间字符串,失败返回0,成功返回截取的字符串。

<?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;
}
?>
Copy after login

希望本文所述对大家的php程序设计有所帮助。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!