Home >Backend Development >PHP Tutorial >php正则匹配问题

php正则匹配问题

WBOY
WBOYOriginal
2016-06-23 13:52:07970browse

$t='

chrome
test
';
$pre_reg = '/ .*? preg_match_all($pre_reg,$t,$matches);
print_r($matches);

$t我省略了其他的,只留下了主要信息,但即便如此还是无法输出,请问这是什么问题
(我的原意是匹配 chrome 中的D8%C1%D0%CB%B9和chrome)


回复讨论(解决方案)

$pre_reg = '/

.*?

$t='<tr><td><a href="/f?kw=chrome" title="chrome">chrome</a></td><tr><td><a href="/f?kw=%CA%A5%B0%B2%B5%" title="test">test</a></td>';$find='/href="\/f\?kw=([^"]*?)"\stitle="([^"]*?)"/s';preg_match_all($find,$t,$res);echo "<pre class="brush:php;toolbar:false">";print_r($res);echo "
";/*Array(    [0] => Array        (            [0] => href="/f?kw=chrome" title="chrome"            [1] => href="/f?kw=%CA%A5%B0%B2%B5%" title="test"        )    [1] => Array        (            [0] => chrome            [1] => %CA%A5%B0%B2%B5%        )    [2] => Array        (            [0] => chrome            [1] => test        ))*/

$t='<tr><td><a href="/f?kw=chrome" title="chrome">chrome</a></td><tr><td><a href="/f?kw=%CA%A5%B0%B2%B5%" title="test">test</a></td>';$find='/href="\/f\?kw=([^"]*?)"\stitle="([^"]*?)"/s';preg_match_all($find,$t,$res);echo "<pre class="brush:php;toolbar:false">";print_r($res);echo "
";/*Array(    [0] => Array        (            [0] => href="/f?kw=chrome" title="chrome"            [1] => href="/f?kw=%CA%A5%B0%B2%B5%" title="test"        )    [1] => Array        (            [0] => chrome            [1] => %CA%A5%B0%B2%B5%        )    [2] => Array        (            [0] => chrome            [1] => test        ))*/


似乎还是不对,这是我原本的数据:

nbsp;html> 管理我喜欢的吧_百度贴吧 
<script></script>
<script>var TbConf = {'domain' : {"TB_STATIC":"http://static.tieba.baidu.com/","TB":"http://tieba.baidu.com/","PASSPORT":"http://passport.baidu.com/","MSG":"http://msg.baidu.com/","SPACE":"http://hi.baidu.com/","UFACE":"http://tb.himg.baidu.com/","IMGSRC":"http://imgsrc.baidu.com/","ZYQ_AD":"http://a.baidu.com/","ZYQ_SMALL_PIC":"http://imgsrc.baidu.com/forum/abpic/item/","TB_UPLOAD":"http://upload.tieba.baidu.com/"}};var Tbs = "b62b84c39c54e4971407421926";</script>
吧名  经验值  等级  取消关注 
chrome 6960
Chrome+
12
php正则匹配问题 
php正则匹配问题gtabbs 5740
水帝
11
php正则匹配问题 
圣安地列斯 5096
追踪到底
11

我需要的内容就是这两个xx的内容,请问如何匹配

是对的啊

$str=file_get_contents('test.txt');$find='/href="\/f\?kw=([^"]*?)"\stitle="([^"]*?)"/s'; preg_match_all($find,$str,$res); echo "<pre class="brush:php;toolbar:false">";print_r($res);echo "
";/*Array(    [0] => Array        (            [0] => href="/f?kw=chrome" title="chrome"            [1] => href="/f?kw=gtabbs" title="gtabbs"            [2] => href="/f?kw=%CA%A5%B0%B2%B5%D8%C1%D0%CB%B9" title="圣安地列斯"        )    [1] => Array        (            [0] => chrome            [1] => gtabbs            [2] => %CA%A5%B0%B2%B5%D8%C1%D0%CB%B9        )    [2] => Array        (            [0] => chrome            [1] => gtabbs            [2] => 圣安地列斯        ))*/

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