Home > Backend Development > PHP Tutorial > 贪婪模式正则匹配的问题

贪婪模式正则匹配的问题

WBOY
Release: 2016-06-06 20:47:01
Original
1051 people have browsed it

正文:

<code>$content = 'http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';
</code>
Copy after login
Copy after login

期望得到:

<code>[0] => http://xx.uz.taobao.com 
[1] => http://yy.uz.taobao.com
</code>
Copy after login
Copy after login

方法一:

<code>$pat = '/http:\/\/(.*)\.uz\.taobao\.com/is';
</code>
Copy after login
Copy after login

结果:

<code>[0] => http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com 
</code>
Copy after login
Copy after login

方法二:

<code>$pat = '/http:\/\/(.*)\.uz\.taobao\.com/iUs';
</code>
Copy after login
Copy after login

结果:

<code>[0] => http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com 
[1]=>http://yy.uz.taobao.com 
</code>
Copy after login
Copy after login

请问想得到我期望的值应该怎么写呢?

回复内容:

正文:

<code>$content = 'http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';
</code>
Copy after login
Copy after login

期望得到:

<code>[0] => http://xx.uz.taobao.com 
[1] => http://yy.uz.taobao.com
</code>
Copy after login
Copy after login

方法一:

<code>$pat = '/http:\/\/(.*)\.uz\.taobao\.com/is';
</code>
Copy after login
Copy after login

结果:

<code>[0] => http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com 
</code>
Copy after login
Copy after login

方法二:

<code>$pat = '/http:\/\/(.*)\.uz\.taobao\.com/iUs';
</code>
Copy after login
Copy after login

结果:

<code>[0] => http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com 
[1]=>http://yy.uz.taobao.com 
</code>
Copy after login
Copy after login

请问想得到我期望的值应该怎么写呢?

<code class="lang-php">$content = 'http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';

preg_match_all('/(https?:\/\/[^.]+\.uz\.taobao\.com)/i', $content, $match);
print_r($match[1]);
</code>
Copy after login

so easy.

<code><?php $content = 'http://www.taobao.com  http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';
$pattern = '/http:\/\/[^\/]*?\.uz\.taobao\.com/is';

preg_match_all($pattern, $content, $result);
var_dump($result);

// output
array(1) {
  [0] =>
  array(2) {
    [0] =>
    string(23) "http://xx.uz.taobao.com"
    [1] =>
    string(23) "http://yy.uz.taobao.com"
  }
}
</code>
Copy after login

<code>$content = '<a href="http://www.taobao.com"> sdfasdfasfas sdfsfasf http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';
$urls = explode('http://', $content);
array_shift($urls);
$res = [];
foreach($urls as &$url) {
    $item = explode('taobao.com', $url);
    $url = "http://{$item[0]}taobao.com";
    if(preg_match('/uz\.taobao\.com/', $url))
        $res[] = $url;
}
print_r($res);
//Array ( [0] => http://xx.uz.taobao.com [1] => http://yy.uz.taobao.com )
</a></code>
Copy after login
<code>$content = '<a href="http://www.taobao.com"> sdfasdfasfas sdfsfasf http://mai.taobao.com http://xx.uz.taobao.com http://yy.uz.taobao.com ';
preg_match_all('/(http[^taobao]*?uz\.taobao\.com)/i', $content, $match);
print_r($match[1]);
//Array ( [0] => http://xx.uz.taobao.com [1] => http://yy.uz.taobao.com )
</a></code>
Copy after login

另有一个小建议是:一步分成两步,先把URL分拆,再对每个URL做匹配。这样可以增强语义的直观性。

Related labels:
php
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