Question

I have a string like this:

$data = 'id=1

username=foobar

comment=This is

a sample

comment';

And I would like to remove the \n in the third field (comment=...).

I have this regular expression that serves my purpose but not so well:

preg_replace('/\bcomment=((.+)\n*)*$/', "comment=$2 ", $data);

My problem is that every match within the second group overwrites the previous match. Thus, instead of having this:

'...
comment=This is a sample comment'

I ended up with this:

'...
comment= comment'

Is there any way of storing the intermediate backreferences in a regular expression? Or do I have to match every occurrence inside a loop?

Thanks!

Was it helpful?

Solution

This:

<?php
$data = 'id=1

username=foobar

comment=This is

a sample

comment';

// If you are at PHP >= 5.3.0 (using preg_replace_callback)
$result = preg_replace_callback(
    '/\b(comment=)(.+)$/ms',
    function (array $matches) {
        return $matches[1] . preg_replace("/[\r\n]+/", " ", $matches[2]);
    },
    $data
);

// If you are at PHP < 5.3.0 (using preg_replace with e modifier)
$result = preg_replace(
    '/\b(comment=)(.+)$/mse',
    '"\1" . preg_replace("/[\r\n]+/", " ", "\2")',
    $data
);

var_dump($result);

will give

string(59) "id=1

username=foobar

comment=This is a sample comment"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top