2012-07-12 81 views
0

如果我已經在我的php變量中聲明瞭數據,我怎樣才能從santance從「any」複製到「Would」。PHP變量「如何從變量中複製特定數據」

數據舉例:

如果您有關於麥克米倫任何問題,我們將愛收到你的來信。

感謝您的幫助。

親切的問候, 卷軸。

+0

'substr()'或'strpos()'或正則表達式的組合。你的問題太具體,不能回答,除非你真的想在這兩個確切的單詞之間的短語。 – Matthew 2012-07-12 02:00:51

+0

「any」和「would」之間的內容是否可能發生變化,或者始終保持不變? – 2012-07-12 02:01:03

+0

其始終相同... – Reel 2012-07-12 02:03:05

回答

0

你可以試試這個:

$string = 'If you have any questions about Macmillan we would love to hear from you.'; 

preg_match('/(any)(.*?)(would)/', $string, $matches); 

echo $matches[0]; // any questions about Macmillan we would 

編輯1

要匹配不止一個結果,如果存在:

preg_match_all('/(any)(.*?)(would)/', $string, $matches); 

foreach($matches[0] as $value) 
    echo 'Found: ' . $value . '<br />'; 
+0

它將匹配'任何人':http://viper-7.com/AExVlS – Ruel 2012-07-12 02:12:13

+0

作品謝謝.... – Reel 2012-07-12 02:13:29

+0

@Ruel,感謝通知,我做了一個編輯。 – 2012-07-12 02:17:13

1

使用preg_match()

$somestr = 'If you have any questions about Macmillan we would love to hear from you.'; 
if (preg_match('/(\bany\b.+?\bwould\b)/i', $somestr, $matches)) { 
    echo $matches[0]; 
}