John Magnolia
John Magnolia

Reputation: 16793

Get everything between strings and replace with something else

I am trying to get everything between the {code} tags from the $html string.

So far I have written this but this doesn't work as expected, only replacing the first {code}

Also I would like this to work for many code tags, but haven't gotten this far yet.

<?php    

$html = <<< EOT
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>{code}My test code 1{/code}</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> 
EOT;

$startPos = strpos($html, '{code}');
$endPos = strpos($html, '{/code}');
if($startPos !== false && $endPos !== false){
    $startPos += 6; # strlen('{code}')
    $endPos += 7; # strlen('{/code}')

    // get code
    $code = substr($html, $startPos, $endPos);

    // remove all code apart from start {code}
    $html = substr($html, $startPos-6, $endPos);

    // replace new code
    $new_code = 'test';
    $html = str_replace('{code}', $new_code, $html);
}

echo $html;

Result: - http://codepad.viper-7.com/T9sbbN

testMy test code 1{/code}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Expected Result

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

test

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Upvotes: 0

Views: 93

Answers (2)

Arian Faurtosh
Arian Faurtosh

Reputation: 18491

This works for one {code}.

<?php

$html = <<< EOT
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>{code}My test code 1{/code}</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> 
EOT;

$startPos = strpos($html, '{code}');
$endPos = strpos($html, '{/code}');

if($startPos !== false && $endPos !== false){
    $startPos += 6; # strlen('{code}')


    // get code
    $code = substr($html, $startPos, $endPos-$startPos);


    // replace new code
    $new_code = 'test';
    $html = str_replace('{code}'.$code.'{/code}', $new_code, $html);
}

echo $html;

Multiple could look like this:

<?php

$html = <<< EOT
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>{code}My test code 1{/code}</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.{code}hi{/code}</p> 
EOT;

$html = test($html);

echo $html;

function test($html){
    do{
        $startPos = strpos($html, '{code}');
        $endPos = strpos($html, '{/code}');

        if($startPos !== false && $endPos !== false){
            $startPos += 6; # strlen('{code}')

            // get code
            $code = substr($html, $startPos, $endPos-$startPos);

            // replace new code
            $new_code = 'test';
            $html = str_replace('{code}'.$code.'{/code}', $new_code, $html);
        }

    }while($startPos !== false && $endPos !== false);


    return $html;
}

Upvotes: 1

b0zar
b0zar

Reputation: 41

regexp exsample

$html = preg_replace('/{code}.*{\/code}/', 'test', $html);

with your code: here

check out cool reg exp builder:regexper

Upvotes: 1

Related Questions