替换一个特定单词的出现,如果没有被另一个特定单词继续


Replacing occurences of a specific word if not proceeded by another specific word?

我有一些文本,比如:

*open* blah blah blah blah blah *close* blah blah *open* blah blah *close* blah blah *close*

我想知道如何删除/替换没有由*open*进行的*close*的任何出现。

那么上面的文本将变成:

*open* blah blah blah blah blah *close* blah blah *open* blah blah *close* blah blah

我想也许使用regex preg_replace -但我的regex技能不是那么强?

<?php
$string = "*open* blah blah blah blah blah *close* blah blah *open* blah blah *close* blah blah *close*";
$string = preg_replace('#(?<!'*open'*)'*close'*#', '', $string); //this only works for immediate proceedings
echo($string);
?>

不使用正则表达式也可以通过以下代码实现:

$openTag = '*open*';
$closeTag = '*close*';
$openTagLength = mb_strlen($openTag);
$closeTagLength = mb_strlen($closeTag);
$subj = '*open* blah blah blah blah blah *close* blah blah *open* blah blah *close* blah blah *close*';
$len = mb_strlen($subj);
$isOpened = false;
$res = '';
for ($i = 0; $i < $len; )
{
    if (mb_substr($subj, $i, $openTagLength) === $openTag) {
        // found open tag
        $res .= $openTag;
        $isOpened = true;
        $i += $openTagLength;
    } elseif (mb_substr($subj, $i, $closeTagLength) === $closeTag) {
        // found close tag
        if ($isOpened) {
            $res .= $closeTag;
        } // else skip
        $isOpened = false;
        $i += $closeTagLength;
    } else {
        // non-tag
        $res .= mb_substr($subj, $i, 1);
        $i++;
    }
}
echo $res;

试一下:

    $pattern = "/(''*open''*.*?''*close''*)/";
    $target = "*close* *close* *open* blah blah blah blah blah *close* blah blah *open* blah blah *close* blah blah *close* *close* *open* *open* *close* ";
    $prevMatchEndIndex = 0;
    $matches = array();
    $lastMatchEndIndex = 0;
    $resultParts = array();
    while(preg_match($pattern, $target, $matches, PREG_OFFSET_CAPTURE, $prevMatchEndIndex)) {
        $matchedString = $matches[0][0];
        $matchStartIndex = $matches[0][1];
        $matchEndIndex = $matchStartIndex + strlen($matchedString) + 1;
        $unmatchedString = substr($target, $prevMatchEndIndex, $matchStartIndex - $prevMatchEndIndex);
        $unmatchedString = preg_replace("/''s*''*close''*''s*/", " ", $unmatchedString);
        $resultParts[] = trim($unmatchedString);
        $resultParts[] = trim($matchedString);
        $prevMatchEndIndex = $matchEndIndex;
        $lastMatchEndIndex = $matchEndIndex;
    }
    $lastUnmatchedPart = substr($target, $lastMatchEndIndex);
    $lastUnmatchedPart = preg_replace("/''s*''*close''*''s*/", " ", $lastUnmatchedPart);
    $resultParts[] = $lastUnmatchedPart;    
    echo $target . "<br />";
    echo join($resultParts, " ");
相关文章: