Question

I have some text like:

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

I was wondering how would I remove/replace any occurrences of *close* which are not proceeded by *open*.

So the above text would become:

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

I though of maybe using regex with preg_replace - but my regex skills are not that strong?

<?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);
?>

Code examples would be welcome.

Was it helpful?

Solution

It can be done without regex via the following code:

$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;

OTHER TIPS

Try this out:

    $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, " ");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top