2016-10-14 4 views
0

ich viele Links haben:PHP vergleichen mehrere String

http://exmpale.com/abc/exmpale.html 
http://exmpale.com/abc/exmpale1.html 
http://exmpale.com/abc/exmpale2.html 
http://exmpale.com/abc/exmpale/exmpale1.html 
http://exmpale.com/abc/exmpale/exmpale2.html 
http://exmpale.com/abc/exmpale/exmpale3.html 
http://exmpale.com/abcd/exmpale1.html 
http://exmpale.com/abcd/exmpale2.html 
http://exmpale.com/abcd/exmpale2.html 
http://exmpale.com/abcd/exmpale4.html 
http://exmpale.com/abc/abc/exmpale1.html 
http://exmpale.com/abc/abc/exmpale2.html 
http://exmpale.com/abc/abc/exmpale3.html 
http://exmpale.com/abc/abc/exmpale4.html 
http://exmpale.com/abc/abc/exmpale5.html 

ich php, ich will alle Links wie http://exmpale.com/abc/(*).html bekommen und es in Array-Liste. Es tut mir leid, ich habe Code vergessen. Code von mir

$a = array(
    0 => 'http://exmpale.com/abc/exmpale.html', 
    1 => 'http://exmpale.com/abc/exmpale1.html', 
    2 => 'http://exmpale.com/abc/exmpale2.html', 
    3 => 'http://exmpale.com/abc/exmpale/exmpale1.html', 
    4 => 'http://exmpale.com/abc/exmpale/exmpale2.html', 
    5 => 'http://exmpale.com/abc/exmpale/exmpale3.html', 
    6 => 'http://exmpale.com/abcd/exmpale1.html', 
    7 => 'http://exmpale.com/abcd/exmpale2.html', 
    8 => 'http://exmpale.com/abcd/exmpale2.html', 
    9 => 'http://exmpale.com/abcd/exmpale4.html', 
    10 => 'http://exmpale.com/abc/abc/exmpale1.html', 
    11 => 'http://exmpale.com/abc/abc/exmpale2.html', 
    12 => 'http://exmpale.com/abc/abc/exmpale3.html', 
    13 => 'http://exmpale.com/abc/abc/exmpale4.html', 
    14 => 'http://exmpale.com/abc/abc/exmpale5.html', 
); 

foreach($a as $k => $v){ 
    $abc = preg_match_all('/http\:\/\/exmpale\.com\/abc\/(.*?)\.html/',$v,$b); 
    print_r($b[1]); 
} 

und ich möchte das Ergebnis erhalten.

$result = array(
    'http://exmpale.com/abc/exmpale.html', 
    'http://exmpale.com/abc/exmpale1.html', 
    'http://exmpale.com/abc/exmpale2.html'); 

aber es dauerte alles Ergebnis, bitte hilf mir dankt Ihnen.

+0

Ich schlage vor, Sie werfen Sie einen Blick auf Loops und Teilstring-Funktionen. Fangen Sie an, in die Dokumentation zu schauen, fangen Sie an, Google dafür zu verwenden. Dann starte dein eigenes Skript, versuche es selbst zu lösen. _Then_, wenn Sie auf ein _specific_ Problem mit Ihrem eigenen Code stoßen, _then_ ist die Zeit, hier zu fragen und Ihren Code innerhalb der Frage zu veröffentlichen. Wir sind nicht hier, um deine Arbeit für dich zu tun. Wir sind hier, um Ihnen zu helfen, Ihre Arbeit zu erledigen. – arkascha

+0

Entschuldigung, ich Frogot Code von mir – phuochq

Antwort

1

Verwenden Sie einfach die PHP-Funktion preg_match_all, und die übereinstimmende Zeichenfolge wird in ein Array geladen, das Sie der obigen Funktion als Argument übergeben. Bitte lesen Sie das PHP-Handbuch auf preg_match_all und erstellen Sie eine Regex für Ihre Bedürfnisse. Sie können Ihre regex here

+0

Ich benutze Funktion preg_match_all aber Ergebnis nicht wie – phuochq

0

testen Da Sie bieten nicht, welche Art von Datenstruktur werden die URLs gespeichert, gehe ich davon aus, es ist eine rohe string:

$data_raw = "http://exmpale.com/abc/exmpale.html 
http://exmpale.com/abc/exmpale1.html 
http://exmpale.com/abc/exmpale2.html 
http://exmpale.com/abc/exmpale/exmpale1.html 
http://exmpale.com/abc/exmpale/exmpale2.html 
http://exmpale.com/abc/exmpale/exmpale3.html 
http://exmpale.com/abcd/exmpale1.html 
http://exmpale.com/abcd/exmpale2.html 
http://exmpale.com/abcd/exmpale2.html 
http://exmpale.com/abcd/exmpale4.html 
http://exmpale.com/abc/abc/exmpale1.html 
http://exmpale.com/abc/abc/exmpale2.html 
http://exmpale.com/abc/abc/exmpale3.html 
http://exmpale.com/abc/abc/exmpale4.html 
http://exmpale.com/abc/abc/exmpale5.html"; 

$urls = explode("\n", $data_raw); 

$accept_host = "exmpale.com"; 
$accept_path = "/abc/"; 

$filtered_urls = array_filter($urls, function($url) use($accept_host, $accept_path) { 
    $parts = parse_url($url); 
    return $parts['host'] == $accept_host 
     && strpos($parts['path'], $accept_path) === 0 
     && strpos($parts['path'], '/', strlen($accept_path)) === false; 
}); 

printf("<pre>%s</pre>", print_r($filtered_urls, true)); 

Ausbeuten:

Array 
(
    [0] => http://exmpale.com/abc/exmpale.html 
    [1] => http://exmpale.com/abc/exmpale1.html 
    [2] => http://exmpale.com/abc/exmpale2.html 
) 
+0

danke Ihnen. aber Daten in der Zeichenfolge und ich möchte Ergebnis erhalten http://exmpale.com/abc/exmpale.html, http://exmpale.com/abc/exmpale1.html und http://exmpale.com/abc/exmpale2. html – phuochq

+0

Ich verstehe, dass Sie jede URL getrennt durch ein Komma wollen. Sehen Sie sich das Update an. – msfoster

+0

Ich habe gerade dein Update gesehen. Das Ergebnis, nach dem Sie fragen, ist das Array $ filtered_urls. Hast du den Code ausprobiert? – msfoster