2015-05-23 148 views
0

我是初學者到PHP,我不知道如何解決此問題。獲取特定字符串,包括特定字符之間的特定字 - php

我有一個包含段落和鏈接的文本文件,這是從文本文件的例子:

$string = ("Lorem 'Ipsum' is simply: dummy 'http://www.exmplelink1.com/blah/blah/file 1 b.txt' text of the printing 'http://www.exmplelink1.com/blah/blah/file 1 c.txt' and typesetting industry. Lorem Ipsum 'http://www.exmplelink2.com/blah/file.txt' has been, the 'industry's standard'"); 

請注意,有很多單引號和二級域名的字符串:exmplelink1和exmplelink2

  • 我怎麼只得到exmplelink1鏈接(單引號之間的完整鏈接)有兩個環節在這種情況下:

    'http://www.exmplelink1.com/blah/blah/file 1 b.txt' 
    'http://www.exmplelink1.com/blah/blah/file 1 c.txt' 
    

感謝您的幫助:)

+1

的可能重複[找到所有的URL(鏈接)文本與PHP](http://stackoverflow.com/questions/6065362/find-all-urls-links-in-text-with-php) – Saty

+0

使用正則表達式。像''http。*?''應該可以工作。 – fzzfzzfzz

+0

我可能會添加,我真的不知道正則表達式:( – poxoson

回答

1

這會爲你工作,只得到exmplelink1,使用file_get_contents閱讀你提交的數據作爲串&然後用下面的代碼,讓你期望的exmplelink1

$re = "/'http:\\/\\/www\\.exmplelink1.*?'/m"; 
$str = ("Lorem 'Ipsum' is simply: dummy 'http://www.exmplelink1.com/blah/blah/file 1 b.txt' text of the printing 'http://www.exmplelink1.com/blah/blah/file 1 c.txt' and typesetting industry. Lorem Ipsum 'http://www.exmplelink2.com/blah/file.txt' has been, the 'industry's standard'"); 

preg_match_all($re, $str, $matches); 
echo '<pre>'; 
print_r($matches); 
echo '</pre>'; 

見正則表達式https://regex101.com/r/gE5uX6/2

+0

工作的鏈接,你是男人,上帝保佑你:) – poxoson

+0

@poxoson,很高興它爲你工作,祝你好運。 –

+0

謝謝你是陽光明媚的:) – poxoson