views:

469

answers:

3

I have the following string replacement problem and I am in quite a fix here

PFB the sample string

$string = 'The quick sample_text_1 56 quick sample_text_2 78 fox jumped over the lazy dog.';

$patterns[0] = '/quick/';
$patterns[1] = '/quick/';
$patterns[2] = '/fox/';

$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';

echo preg_replace($patterns, $replacements, $string);

I need to replace 'quick' depending on the numbers i send

i.e if my input to a function is 56, the quick before 56 needs to be replaced with bear and if my input to a function is 78, the quick before 78 needs to be replaced with black

Can someone please help me with this?

A: 

Instead of working with preg_replace, use substr_replace to do your string replacement and strpos to find the start and end points within the string based on the parameters you pass. Your pattern is a simple string, so it doesn't require a regular expression, and substr_replace will allow you to specify a start and end point within the string to do replacements (which seems to be what you're looking for).

EDIT:

Based on your comment, it sounds like you have to do a lot of checking. I haven't tested this, so it may have a bug or two, but try a function like this:

function replace($number, $pattern, $replacement)
{
    $input = "The quick sample_text_1 56 quick sample_text_2 78 fox jumped over the lazy dog.";
    $end_pos = strpos($input, $number);
    $output = "";
    if($end_pos !== false && substr_count($input, $pattern, 0, $end_pos))
    {
     $start_pos = strrpos(substr($input, 0, $end_pos), $pattern);
     $output = substr_replace($input, $replacement, $start_pos, ($start_pos + strlen($pattern)));
    }
    return $output;
}

This function does the following:

  1. First, check that the "number" parameter even exists in the string ($end_pos !== false)
  2. Check that your pattern exists at least once in between teh beginning of the string and the position of the number (substr_count($input, $pattern, 0, $end_pos))
  3. Use strrpos function to get the position of the last occurrence of the pattern within the substring
  4. Use the start position and the length of the pattern to insert your replacement string using substr_replace
Rich
Hi rich,This was just a sample string and I do not know start part as the inputs are the number,string to be replaced and the replacing string.I tried it using substr_replace and strpos but came back to square
gnanesh
+1  A: 

I think regular expressions will make this difficult but you should be able to do it using only strpos, substr and str_replace.

  • Use strpos to find the location in the string of 56 and 78.

  • Then cut the string up into substrings at these points using substr.

  • Now, replace 'quick' with the correct variable, depending on whether 56 or 78 was sent to the function and which substring you are dealing with.

Jon Winstanley
A: 

You are doing it the wrong way. Instead depending on your function input you should use the correct find and replace values. Just create a map of find and replace values depending on your function input value. Like:

$map = array( 56 => array('patterns' => array(), 'replacements' => array()), 78 => array(...) );