JackWill
JackWill

Reputation: 53

str_replace only replace specific occurence?

I have this SQL command:

$sql = "INSERT INTO foo (value, value2, value3) 
        VALUES (value1, value2, value3)";

And I want to replace the first ) with , custom_value), Similarly the second ) with , 1234567890)

The result should be like:

$sql = "INSERT INTO foo (value, value2, value3, custom_value) 
        VALUES (value1, value2, value3, 1234567890)";

I tried replacing all occurrences using str_replace().

Is there something with a option to replace only a specific occurrence?

Upvotes: 1

Views: 639

Answers (5)

Kaii
Kaii

Reputation: 20540

There is no such thing in the basic function set of PHP. But you can implement it using substr_replace().

$sql = "foo (bar, baz, blub) bar (boo, far, faz)";

// only replace first occurance
$firstpos = strpos($sql, ')');
$sql = substr_replace($sql, ', custom_value)', $firstpos, 1);

// only replace second occurance
$secondpos = strpos( $sql, ')', strpos( $sql, ')' ) + 1);
$sql = substr_replace( $sql, ', 123456)', $secondpos, 1);

echo $sql, PHP_EOL;

Results:

foo (bar, baz, blub, custom_value) bar (boo, far, faz, 123456)

Rewritten as a reusable function:

function str_replace_occurance($search, $replace, $subject, $occurance) {
   $pos = NULL;
   for ($i = 1; $i <= $occurance; $i++) {
       $pos = strpos($subject, $search, ($pos===NULL) ? 0 : $pos+1);
   }
   return substr_replace($subject, $replace, $pos, strlen($search));
}

$sql = str_replace_occurance(')', ', custom_value)', $sql, 1);
$sql = str_replace_occurance(')', ', 1234567890)', $sql, 2);
echo $sql, PHP_EOL;

Edit: reordered parameters to have the same order and name as the original str_replace

Upvotes: 1

SERPRO
SERPRO

Reputation: 10067

For the example you gave.. you can you use this regex:

#(\).+?\(.+)(\))#

In PHP code, it looks like this:

$sql = preg_replace("#(\).+?\(.+)(\))#", 
                    ", custom_value\1, 1234567890\2", 
                    $sql, -1, $count);

it will search for a closed parenthesis followed by an open parenthesis, and after that will look for the latest closed parenthesis

Example: http://xrg.es/#b1uu3f

Upvotes: 1

Andreas Wong
Andreas Wong

Reputation: 60536

Try the following:

$sql = str_replace('value3', 'value3, %s', $sql);
$sql = sprintf($sql, 'custom_value', '1234567890');

Test on my local setup:

$sql = '(value, value2, value3) (value1, value2, value3)';
$sql = str_replace('value3', 'value3, %s', $sql);
$sql = sprintf($sql, 'custom_value', '1234567890');

// produces (value, value2, value3, custom_value) (value1, value2, value3, 1234567890)
echo $sql; 

Here is a reusable function:

function replace_occurence($search, $replace, $subject) {
        $subject = str_replace($search, '%s', $subject);
        array_unshift($replace, $subject);
        return call_user_func_array('sprintf', $replace);
}
$sql = '(value, value2, value3) (value1, value2, value3)';
$sql = replace_occurence(')', array(', custom_value)', ', 123456790)'), $sql);

Upvotes: 1

KoolKabin
KoolKabin

Reputation: 17663

If using only str_replace then str_replace with limit for 1st and again replace it for second one...

http://au.php.net/manual/en/function.str-replace.php

Upvotes: -1

slash197
slash197

Reputation: 9034

If you have php > 5.0 you can use count in str_replace to set how many replacements you need.

Upvotes: -1

Related Questions