首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用RegEx检测引号中的引号

使用RegEx检测引号中的引号
EN

Stack Overflow用户
提问于 2018-06-02 16:35:15
回答 1查看 62关注 0票数 1

我正在寻找一种方法来检测和删除引号中的引用,例如: something "something " something “something" something。

如你所见,在上面的例子中,斜体的东西被用双引号括起来。我想从这些外引号中去掉里面的字符串。

因此,表达式应该简单地查找引号,引号之间有一个文本加上另一组文本换行文本,然后删除最后一个换行文本的引号。

这是我当前的代码(php):

代码语言:javascript
复制
    preg_match_all('/".*(".*").*"/', $text, $matches);
    if(is_array($matches[0])){
        foreach($matches[0] as $match){
            $text = str_replace($match, '"' . str_replace('"', '', $match) . '"', $text);
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2018-06-04 01:29:34

您可以利用带有第三个参数(offset)的strpos()来查找所有引号,并将所有引号从1替换为n-1:

代码语言:javascript
复制
<?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;
?>

这就产生了

代码语言:javascript
复制
something "something something something something" something

您甚至可以将其隐藏在一个类中:

代码语言:javascript
复制
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]] = "";
            }
        }

    }
}

并使用以下命令调用它:

代码语言:javascript
复制
$q = new unquote();
$data = $q->cleanData($data);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50655006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档