2009-09-02 25 views

回答

8

http://www.php.net/preg_match

<?php 
$x = 'This User "The Title Of The Post"'; 

preg_match('/".*?"/', $x, $matches); 

print_r($matches); 

/* 
    Output: 
    Array 
    (
     [0] => "The Title Of The Post" 
) 

*/ 
?> 
+0

和我想到的一樣。 :-) – 2009-09-02 02:55:01

+0

不全球:, - ( – 2009-09-02 02:55:22

+1

海報要求「只是引號內部的內容」,並且接受的解決方案包含引號,我的解決方案不包括引號作爲海報請求 – Asaph 2009-09-03 04:40:09

1
<?php 

$string = 'This User "The Title Of The Post"'; 

preg_match_all('/"([^"]+)"/', $string, $matches); 

var_dump($matches); 
1
$string = 'This user "The Title Of The Post"'; 

$its_a_match = preg_match('/"(.+?)"/', $string, $matches); 
$whats_inside_the_quotes = $matches[1]; 

$its_a_match1,如果它做了一個成功的匹配,否則0$whats_inside_the_quotes將包含正則表達式中的一組括號中匹配的字符串。

如果有點不清楚(實際情況),preg_match()實際上給出的值爲$matches(第三個參數)。

1

$str = 'This User "The Title Of The Post"'; 
$matches = array(); 
preg_match('/^[^"]*"([^"]*)"$/', $str, $matches); 
$title = $matches[1]; 
echo $title; // prints The Title Of The Post 
+0

@opello,@Patrick - 你們任何人都可以解釋一下這個答案...我還是新手正則表達式,我想做一個類似的任務 - 「從site_ids == 34 || sales_region == 45'上讀取site_ids =='之後的數字」(即正則表達式應該返回34)。我用'preg_match('/^ [^ site_ids ==] * ||([^ ||] *)「$ /',$ str,$ matches);'在$ str ='site_ids == 34之後''sales_regio n == 45';'而沒有得到結果。我不確定'|'是否需要轉義。問題中的分隔符是一個雙引號('''),而在我的情況下,它們是'site_ids =='和'||'。對我來說什麼是正確的正則表達式?謝謝。 – 2011-06-20 13:43:06