Home  >  Article  >  Backend Development  >  php截取指定2个字符之间字符串的方法_php技巧

php截取指定2个字符之间字符串的方法_php技巧

WBOY
WBOYOriginal
2016-05-16 20:17:251417browse

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

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

使用方法:

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

运行该程序之后:

$need='计组实验';

下面就来完成上面用到的字符串截取函数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;
}
?>

更多关于PHP字符串操作相关内容感兴趣的读者可查看本站专题:《php字符串(string)用法总结

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

Statement:
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