How to Replace and Modify Content Between Two Delimiters in PHP

Posted on August 8, 2009, Filled under PHP,  Bookmark it

A while ago I have written a short tutorial of how you can write a short PHP function to extract content from specific delimiters. I has come to my attention that many people are looking for a way to replace and even modify content between 2 delimiters. Therefore I have decided to write a script that can help them with this endeavor. Uses regular expressions to find and parse the matched content.

Replace all the content found between the delimiters

function replace_content_inside_delimiters($start, $end, $new, $source) {
return preg_replace('#('.preg_quote($start).')(.*)('.preg_quote($end).')#si', '$1'.$new.'$3', $source);
}

As you can see the function uses 4 arguments. The first 2 two are the delimiters (the beginning and the ending one), the 3d is the replacement string and the last one is the main source that is parsed.

$data = '<body><div class="wrap"><div class="inside">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div></div></body>';

$start = '<div class="inside">';
$end =  '</div>';

$replace_with = 'PHP: Hypertext Preprocessor';

$str = replace_content_inside_delimiters($start, $end, 'PHP: Hypertext Preprocessor', $data);

echo $str;

// Result: <body><div class="wrap"><div class="inside">PHP: Hypertext Preprocessor</div></div></body>'

Filter the content found between the delimiters

In case you need to modify the text between the delimiters here’s how you can do it:

$source = '<div class="subtitle">PHP is a widely-used general-purpose scripting language for web development.</div>';

$start = '<div class="subtitle">';
$end = '</div>';

$data = preg_replace('#('.preg_quote($start).')(.*)('.preg_quote($end).')#si', '$1'.parse_content($new).'$3', $source);

function parse_content($content) {
$words = array('PHP', 'scripting', 'development'); // Let's bold some words!

foreach($words as $word) {
$content = str_replace($word, '<strong>'.$word.'</strong>', $content);
}

return $content;
}

How it works?

The regex uses the \s and the \i modifiers. The former (aka: DOTALL) makes dot a special character that matches newlines too. The later matches the characters in insensitive mode.

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

One Reply to "How to Replace and Modify Content Between Two Delimiters in PHP"

  1. hi

    this seems to not work if i do a replacement that includes certain values such a number

    for example
    $mystring=”test (1)”;
    replace_content_inside_delimiters(“(“,”)”,”2″,$mystring);

    returns….

    test )

    If I do
    for example
    $mystring=”test (a)”;
    replace_content_inside_delimiters(“(“,”)”,”b”,$mystring);

    that works, returning
    test (b)
    as I would expect.

    Thanks for any help!

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)