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!
- August 8, 2009
- article by Gabriel C.
- 1 comment
Related Posts
How to extract content between two delimiters in PHPat August 29, 2008 with 16 comments
How to emphasize specific words from a string (text)at September 6, 2008
How to Create a PHP Word Popularity Scriptat November 7, 2008 with 6 comments
How to Create an Advanced PHP (bad, naughty) Words Filterat October 26, 2008 with 5 comments
How to replace multiple spaces from a string in PHPat August 29, 2008 with 1 comment

One Reply to "How to Replace and Modify Content Between Two Delimiters in PHP"
November 30, 2009 at 3:14 AM
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!