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

依照要求取值

WBOY
Release: 2016-06-13 12:17:40
Original
858 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...................................等等等.......';<br /><br />preg_match_all('/(TK[PCMR]{2})(\d{5})\w{3}/', $s, $m);<br /><br />$d = array(<br />  'TKPC' => 11069,<br />  'TKMR' => 10999,<br />);<br />foreach($m[1] as $k=>$v) {<br />  if($m[2][$k] <= $d[$v]) {<br />    $s = str_replace($m[0][$k], "<font color=red>{$m[0][$k]}</font>", $s);<br />    $r[] = $m[0][$k];<br />  }<br />}<br />echo $s;
Copy after login
print_r($r);
Copy after login
Array<br />(<br />    [0] => TKPC10090987<br />    [1] => TKPC10098M9Y<br />    [2] => TKMR10059E56<br />    [3] => TKMR10059E56<br />)<br /><br />
Copy after login

------解决思路----------------------
<br />$str='我是一个人TKPC11090987是你TKPC10090987吗?TKPC10098M9Y到底是谁呢?TKMR120990U9TKMR10059E56TKMR10059E56...................................等等等.................................................';<br />$str=preg_replace('/(TKPC<br><font color='#FF8000'>------解决思路----------------------</font><br>TKMR)(\d{5})(.{3})/e',<br />"(('$1'=='TKPC' and $2<=11069) or ('$1'=='TKMR' and $2<=10999))?'$1<span style=\'color:red\'>$2$3</span>':'$0'"<br />,$str);<br />echo $str;<br />
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