我正在寻找一种方法来检测和删除引号中的引用,例如: something "something " something “something" something。
如你所见,在上面的例子中,斜体的东西被用双引号括起来。我想从这些外引号中去掉里面的字符串。
因此,表达式应该简单地查找引号,引号之间有一个文本加上另一组文本换行文本,然后删除最后一个换行文本的引号。
这是我当前的代码(php):
preg_match_all('/".*(".*").*"/', $text, $matches);
if(is_array($matches[0])){
foreach($matches[0] as $match){
$text = str_replace($match, '"' . str_replace('"', '', $match) . '"', $text);
}
}
发布于 2018-06-04 01:29:34
您可以利用带有第三个参数(offset)的strpos()
来查找所有引号,并将所有引号从1替换为n-1:
<?php
$data = <<<DATA
something "something "something something" something" something
DATA;
# set up the needed variables
$needle = '"';
$lastPos = 0;
$positions = array();
# find all quotes
while (($lastPos = strpos($data, $needle, $lastPos)) !== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
# replace them if there are more than 2
if (count($positions) > 2) {
for ($i=1;$i<count($positions)-1;$i++) {
$data[$positions[$i]] = "";
}
}
# check the result
echo $data;
?>
这就产生了
something "something something something something" something
您甚至可以将其隐藏在一个类中:
class unquote {
# set up the needed variables
var $data = "";
var $needle = "";
var $positions = array();
function cleanData($string="", $needle = '"') {
$this->data = $string;
$this->needle = $needle;
$this->searchPositions();
$this->replace();
return $this->data;
}
private function searchPositions() {
$lastPos = 0;
# find all quotes
while (($lastPos = strpos($this->data, $this->needle, $lastPos)) !== false) {
$this->positions[] = $lastPos;
$lastPos = $lastPos + strlen($this->needle);
}
}
private function replace() {
# replace them if there are more than 2
if (count($this->positions) > 2) {
for ($i=1;$i<count($this->positions)-1;$i++) {
$this->data[$this->positions[$i]] = "";
}
}
}
}
并使用以下命令调用它:
$q = new unquote();
$data = $q->cleanData($data);
https://stackoverflow.com/questions/50655006
复制相似问题