我在PHP中有一个字符串,它可以在两种格式之一 - 要么:字符串操作
"example1" AND somethingelse = "example2"
或只是
"example1"
所有我需要的是让数据在语音标记中,无论是还是只是一个。
干杯!
我在PHP中有一个字符串,它可以在两种格式之一 - 要么:字符串操作
"example1" AND somethingelse = "example2"
或只是
"example1"
所有我需要的是让数据在语音标记中,无论是还是只是一个。
干杯!
如果您正在寻找替换文本,按照一定的正则表达式,将有助于:
http://php.net/manual/en/function.preg-replace.php
,或者,如果你不想替换文本,则可以使用与之匹敌:
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
感谢您的帮助,我使用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;
}
看起来有一个问题在那里..某处..我只是不能把我的手指放在它上面。 – 2010-11-29 09:25:36
你如何处理`“然后他说,”这里也有一个引用“”` – stillstanding 2010-11-29 09:27:25
看起来他试图从SQL查询中获取值。 – karim79 2010-11-29 09:27:34