Home > Backend Development > PHP Tutorial > 按照要求取值

按照要求取值

WBOY
Release: 2016-06-23 13:36:58
Original
874 people have browsed it

字符串:
我是一个人TKPC11090987是你TKPC10090987吗?TKPC10098M9Y到底是谁呢?TKMR120990U9TKMR10059E56TKMR10059E56...................................等等等.................................................

要求:
1、截取每一个TKPC和TKMR后的连续5位数。(例中的截取出来就应该是TKPC有11090,12098,TKMR有12099,11059等)
2、将截取后的数字按照如下条件对比:
      2.1   TKPC的两组数字,如果有小于等于11069的话,用红色在原字符串中显示满足条件的每组TKPC+后8位。
      2.1   TKMR的两组数字,如果有小于等于10999的话,用红色在原字符串中显示满足条件的每组TKMR+后8位。
3、取出满足条件的TK**+后8位,存入变量,待其他代码调用。


回复讨论(解决方案)

我的思路,
第一步,explode("TK",$str)
第二步,对于第一步的结果进行遍历判断,strstr($explode,"PC"),strstr($explode,"MR")
第三步,将第二步的符合条件的结果进行截取,substr($substr,2,5)

嗯.....
好像有点累赘了,让高手来解决吧.....

$s = '我是一个人TKPC11090987是你TKPC10090987吗?TKPC10098M9Y到底是谁呢?TKMR120990U9TKMR10059E56TKMR10059E56...................................等等等.......';preg_match_all('/(TK[PCMR]{2})(\d{5})\w{3}/', $s, $m);$d = array(  'TKPC' => 11069,  'TKMR' => 10999,);foreach($m[1] as $k=>$v) {  if($m[2][$k] <= $d[$v]) {    $s = str_replace($m[0][$k], "<font color=red>{$m[0][$k]}</font>", $s);    $r[] = $m[0][$k];  }}echo $s;
Copy after login
print_r($r);
Copy after login
Array(    [0] => TKPC10090987    [1] => TKPC10098M9Y    [2] => TKMR10059E56    [3] => TKMR10059E56)
Copy after login

$str='我是一个人TKPC11090987是你TKPC10090987吗?TKPC10098M9Y到底是谁呢?TKMR120990U9TKMR10059E56TKMR10059E56...................................等等等.................................................';$str=preg_replace('/(TKPC|TKMR)(\d{5})(.{3})/e',"(('$1'=='TKPC' and $2<=11069) or ('$1'=='TKMR' and $2<=10999))?'$1<span style=\'color:red\'>$2$3</span>':'$0'",$str);echo $str;
Copy after login

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