2010-11-29 279 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; 


}