2017-07-24 2 views
-1
<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> 

wie extrahieren keyword1, keyword2, keyword3, keyword4, keyword5 nur php Array?php pregmatch alle Elemente auf Array

Antwort

0

Wenn die Schlüsselwörter ALLWAYS nach <a href="//examples.com/position/ sind dies die Arbeit tut:

$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]); 

Ausgang:

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

Vielen Dank, es hat funktioniert! – vagiz

0

mit nur preg_match Funktion:

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

var_dump($output_array); 
+0

Ich habe hinzugefügt: preg_match_all ("/ (? <= \/\/examples.com \/position \ /). + (? = [^ \"])/", $ Zeilen, $ output_array); aber es wird nicht zu einem anderen Array-Element nach ersten doppelten Anführungszeichen gespalten – vagiz

0

Sie so etwas tun könnte. Erfassen Sie den href-Wert und den Text des Ankers. Dann bewerten Sie eine Übereinstimmung für den Link. Sollte selbsterklärend sein.

<?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]; 
     } 
    } 
} 

Nicht sicher, ich folge Ihren Kommentaren. Teile gibt es für Ich denke, was Sie brauchen ...

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

Idee ist in Ordnung, aber mein keywordX ist dynamisch, also ich nur einige Schlüsselwörter spulen – vagiz

+0

sorry abotu, dass sie keywordX Zeug dynamisch ist, und auch ich werde nicht einen anderen Teil der href =/Position/sammeln, nur mit href = Domäne/Position/XXXXX bis doppelte Anführungszeichen – vagiz