php pregmatch所有元素到数组

时间:2017-07-24 20:09:54

标签: php url preg-match-all

<a href="/position/memory1"> kw random</a>
<a href="/position/memory2"> kw2 random2</a>
<a href="/position/memory3"> 123 orange</a>
<a href="/position/memory4"> test apple</a>
<a href="/position/memory5"> bla</a>
<div>
    <a href="//examples.com/position/keyword1"> kw random</a>
    <a href="//examples.com/position/keyword2"> kw2 random2</a>
    <a href="//examples.com/position/keyword3" rel="nofollow"> 123 orange</a>
    <a href="//examples.com/position/keyword4"> test apple</a>
    <a href="//examples.com/position/keyword5" title="something"> bla</a>
</div>

如何仅将keyword1keyword2keyword3keyword4keyword5提取到php数组?

3 个答案:

答案 0 :(得分:1)

如果关键字在<a href="//examples.com/position/之后是ALLWAYS,那么就是这样做:

$html = <<<EOD
<a href="/position/memory1"> kw random</a>
<a href="/position/memory2"> kw2 random2</a>
<a href="/position/memory3"> 123 orange</a>
<a href="/position/memory4"> test apple</a>
<a href="/position/memory5"> bla</a>
<div>
    <a href="//examples.com/position/keyword1"> kw random</a>
    <a href="//examples.com/position/keyword2"> kw2 random2</a>
    <a href="//examples.com/position/keyword3" rel="nofollow"> 123 orange</a>
    <a href="//examples.com/position/keyword4"> test apple</a>
    <a href="//examples.com/position/keyword5" title="something"> bla</a>
</div>
EOD;

preg_match_all('~<a href="//examples.com/position/([^"]+)~', $html, $matches);
var_dump($matches[1]);

<强>输出:

array(5) {
  [0]=>
  string(8) "keyword1"
  [1]=>
  string(8) "keyword2"
  [2]=>
  string(8) "keyword3"
  [3]=>
  string(8) "keyword4"
  [4]=>
  string(8) "keyword5"
}

答案 1 :(得分:0)

只需使用preg_match函数:

// $lines is your string
// I think the regex is ok
preg_match_all("/(?<=\/position\/).+(?=\\")/", $lines, $output_array);

var_dump($output_array);

答案 2 :(得分:0)

你可以这样做。捕获href值和锚点文本。然后评估链接上的匹配项。应该是自我解释的。

<?php
 $data = '
<a href="/position/memory1"> Bkw random</a>
<a href="/position/memory2">B kw2 random2</a>
<a href="/position/memory3"> 123 orange</a>
<a href="/position/memory4"> test apple</a>
<a href="/position/memory5"> bla</a>
<a href="//examples.com/position/keyword1"> Akw random</a>
<a href="//examples.com/position/keyword2"> Akw2 random2</a>
<a href="//examples.com/position/keyword3" rel="nofollow"> 123 orange</a>
<a href="//examples.com/position/keyword4"> test apple</a>
<a href="//examples.com/position/keyword5" title="something"> bla</a>
';


$matches = [];
$needles = ['keyword1', 'keyword2', 'keyword3', 'keyword4', 'keyword5'];

preg_match_all('#<a\s+href\s*=\s*"([^"]+)"[^>]*>([^<]+)</a>#i', $data, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {            
    foreach($needles as $needle) {
        if (stristr($match[1], $needle) !== false) {
            echo $match[2];
        }
    }
}

不确定我是否按照您的意见。部件在那里我认为你需要什么......

//           $match[1]              $match[2]
//<a href=" |/position/memory1| "> |Bkw random| </a>