简体   繁体   中英

Writing Elegant PHP Code: Cleaning Up Unnecessary Loops and Merging Arrays

I'm pulling input from six (6) different textareas, exploding the array with \\r\\n as the delimiter, and looping through each set of data twice in order to replace it.

For example, I have textarea inputs where people can put colors similar to the main one listed, so the input for the textarea titled 'Blue' might be Aqua\\r\\nRoyal Blue\\r\\nTeal

Then I have input boxes for Red, Orange, Yellow, Green, and Purple with similar data.

I want to replace the user input -- in this example, Aqua -- with <span class="blue">Aqua</span> . Anything input in the blue box gets wrapped in the 'blue' class, anything from red in class="red" , etc.

I've got this all working fine and well, cycling through each set of data twice: one foreach loop to find the text, another to create the replacement text, and then finally simply using preg_replace to actually perform it the substitution.

$find_blue     = explode ("\r\n", $blue);
$replace_blue  = explode ("\r\n", $blue);

$i = 0;
foreach($replace_blue AS &$html)
{
  $find_blue[$i] = trim($find_blue[$i]);
  $html = '<span class="blue">' . $find_blue[$i] . '</span>';
  $i++;
}
foreach($find_blue AS &$found)
{
  $found = preg_quote(htmlspecialchars($found));
  $found = '~\b('.$found.')\b)~i';
}

$output = preg_replace($find_blue, $replace_blue, $output);

This works swimmingly well, but I think it's clumsy code having 12 loops (2x per color) all doing the same thing. I'd like to write cleaner, more efficient, better code, and I know array_merge exists, but I'm unsure how to integrate it or make everything coherent and cohesive.

You could create a function to process the string, then you only need set the values of an array.

Something like:

<?php 
/**
 * key = colour to replace
 * value = data received from form
 */
$test_data_set = array(
'red'=>"Auburn\r\nBarn red",
'blue'=>"Aqua\r\nRoyal Blue\r\nTeal",
'green'=>"Amazon\r\nApple green\r\nArmy green",
);

//process
foreach($test_data_set as $color=>$input){
    $t = explode("\r\n",$input);
    array_walk($t, function(&$value, $key, $color){
        $value = '<span class="'.$color.'">' . trim($value) . '</span>';
    }, $color);
    $test_data_set[$color] = $t;
}

//debug
header('Content-Type: text/plain');
echo print_r($test_data_set,true);

/*  Result = $test_data_set
    Array
    (
        [red] => Array
            (
                [0] => <span class="red">Auburn</span>
                [1] => <span class="red">Barn red</span>
            )

        [blue] => Array
            (
                [0] => <span class="blue">Aqua</span>
                [1] => <span class="blue">Royal Blue</span>
                [2] => <span class="blue">Teal</span>
            )

        [green] => Array
            (
                [0] => <span class="green">Amazon</span>
                [1] => <span class="green">Apple green</span>
                [2] => <span class="green">Army green</span>
            )

    )
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM