2010-11-29 188 views
0

我在PHP中有一個字符串,它可以在兩種格式之一 - 要麼:字符串操作

"example1" AND somethingelse = "example2" 

或只是

"example1" 

所有我需要的是讓數據在語音標記中,無論是還是隻是一個。

乾杯!

+2

看起來有一個問題在那裏..某處..我只是不能把我的手指放在它上面。 – 2010-11-29 09:25:36

+0

你如何處理`「然後他說,」這裏也有一個引用「」` – stillstanding 2010-11-29 09:27:25

+0

看起來他試圖從SQL查詢中獲取值。 – karim79 2010-11-29 09:27:34

回答

5
preg_match('/"([^"]*)"/' , $string, $matches) 
# $matches is an array with the results 
# ignore $matches[0] 
# $matches[1] will contain the first string inside double-quotes 
# $matches[2] will contain the second string inside double-quotes (if any) 

更多的信息在這裏:http://www.php.net/manual/en/function.preg-match.php

0

感謝您的幫助,我使用bowsersenior的答案和一些簡單的計數的基礎,稍微向後地管理它!

//Find amount of Speech marks 
$stringCount = substr_count($newstring, '"'); 


if ($stringCount == 2){ 

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

    foreach ($matches as $data) 
    { 
    print_r($data); 
    } 


}else{ 

    //Get first speech marks 

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

    foreach ($matches as $data) 
    { 
    print_r($data); 
    } 


    //Get second speech marks 

    $endString = substr($newstring,-4); 

    echo $endString; 


}