daveycroqet
daveycroqet

Reputation: 2727

Substituting Words in a Body of Text with User Input via Multidimensional Arrays

I have two sets of data stored in multidimensional arrays. One stores regular expressions which will be used to find whole words within a larger body of text:

Array
(
    [red] => Array
        (
            [0] => ~\b(a)\b~i
            [1] => ~\b(b)\b~i
            [2] => ~\b(c)\b~i
        )
    [orange] => Array
        (
            [0] => ~\b(d)\b~i
        )
    [green] => Array
        (
            [0] => ~\b(e)\b~i
        )
)

And the other contains what to replace those matches with:

Array
  (
    [red] => Array
        (
            [0] => <span class="red">A</span>
            [1] => <span class="red">B</span>
            [2] => <span class="red">C</span>
        )
    [orange] => Array
        (
            [0] => <span class="orange">D</span>
        )
    [green] => Array
        (
            [0] => <span class="green">E</span>
        )
)

For exemplary purposes, let's say the body of text is:

The quick brown fox jumps over the lazy dog. a The quick brown fox jumps over the lazy dog. b The quick brown fox jumps over the lazy dog. c The quick brown fox jumps over the lazy dog. d The quick brown fox jumps over the lazy dog. e

The PHP function preg_replace doesn't handle multidimensional arrays, so how would I go about accomplishing this?

Upvotes: 1

Views: 72

Answers (3)

Ja͢ck
Ja͢ck

Reputation: 173522

Assuming both arrays are similarly shaped, simply flatten both arrays like so:

$search = call_user_func_array('array_merge', $search);
$replace = call_user_func_array('array_merge', $replace);

Then perform the replacement:

preg_replace($search, $replace, $string);

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

To deal with your arrays, just use a foreach loop:

foreach($reg_cats as $key=>$reg_cat) {
    $yourstring = preg_replace($reg_cat, $rep_cats[$key], $yourstring);
}

$reg_cats is your array with regex, $rep_cats is your array with replacements. Since they have the same keys and the subarrays the same size, you can do that.

In the case you don't need to transform what you match (here you want to change the letter to uppercase), you can use a single associative array:

$corr = array('<span class="red">$0</span>'    =>  '~\b(?:a|b|c)\b~i',
              '<span class="orange">$0</span>' =>  '~\bd\b~i',
              '<span class="green">$0</span>'  =>  '~\be\b~i');

$result = preg_replace($corr, array_keys($corr), $yourstring);

Upvotes: 1

Jasen
Jasen

Reputation: 12392

you need to turn those into an array it can use. something like this.

$r=array();$s=$r;$n=0;
foreach($Second_array_you_didn_t_name as $kk => $vv)
    foreach($vv as $k => $v)
    {
      $r[++$n]=$v;
      $s[$n]=$First_array_you_didn_t_name[$kk][$k]
    }
$tqbf="The quick brown fox jumps over the lazy dog. a The quick brown fox jumps over the lazy dog. b The quick brown fox jumps over the lazy dog. c The quick brown fox jumps over the lazy dog. d The quick brown fox jumps over the lazy dog. e";
print preg_replace($r,$s,$tqbf);

Upvotes: 0

Related Questions