用数组值替换分隔符之间的每个文本出现(在php中)

时间:2016-01-28 17:22:18

标签: php regex preg-replace

我希望替换

之间的每次出现
{{

}}

我想使用标志{{}}内的old_text来选择新的, 我有这个阵列:

$var["blue"]="ocean";
$var["red"]="fire";
$var["green"]="forest";

案文可能是:

people loves {{blue}} looks {{red}} and enjoy {{green}}

应该成为

people loves ocean looks fire and enjoy forest

我尝试使用preg_replace但没有结果:

function replaceTags($startPoint, $endPoint, $newText, $source) { 
    $var["blue"]="ocean";
    $var["red"]="fire";
    $var["green"]="forest";

    return preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#si', 'IDONT-KNOW-WHAT-PUT-HERE', $source);
}

3 个答案:

答案 0 :(得分:3)

这是PHP函数preg_replace_callback()的完美示例:

$var = array(
    'blue'  => 'ocean',
    'red'   => 'fire',
    'green' => 'forest',
);
$template = 'people loves {{blue}} looks {{red}} and enjoy {{green}}';

$text = replaceTags('{{', '}}', $template, $var);


/**
 * Replaces the tags in a string using the provided replacement values.
 *
 * @param string $ldelim       the string used to mark the start of a tag (e.g. '{{') 
 * @param string $rdelim       the string used to mark the end of a tag (e.g. '}}') 
 * @param string $template     the string that contains the tags
 * @param array  $replacements the values to replace the tags
 */
function replaceTags($ldelim, $rdelim, $template, array $replacements)
{
    return preg_replace_callback(
        // The 'U' flag prevents the .* expression to be greedy
        // and match everything from the first to the last tag
        '#'.preg_quote($ldelim).'(.*)'.preg_quote($rdelim).'#U',
        function (array $matches) use ($replacements) {
            // $matches contains the text pieces that matches the capture groups
            // in the regexp
            // $matches[0] is the text that matches the entire regexp
            // $matches[1] is the first capture group: (.*)
            $key = $matches[1];
            if (array_key_exists($key, $replacements)) {
                // Replace the tag if a replacement value exists in the list
                return $replacements[$key];
            } else {
                // Don't replace the tag if a value is not assigned for it
                return $matches[0];

                // Alternatively, you can return a default placeholder string
                // or return '' to remove the tag completely
            }
        },
        $template
    );
}

答案 1 :(得分:2)

尝试:

$string = 'people loves {{blue}} looks {{red}} and enjoy {{green}}';

foreach($var as $key => $val){
    str_replace('{{'.$key.'}}', $val, $string);
}

echo $string;

答案 2 :(得分:2)

我也会使用<table> <thead> <tr> <th>id</th> <th>name</th> <th>field</th> <th>facility</th> <th>change</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>name1</td> <td>field1</td> <td>facility1</td> <td>change1</td> </tr> <tr> <td>2</td> <td>name2</td> <td>field2</td> <td>facility2</td> <td>change2</td> </tr> <tr> <td>3</td> <td>name3</td> <td>field3</td> <td>facility3</td> <td>change3</td> </tr> <tr> <td>4</td> <td>name4</td> <td>field4</td> <td>facility4</td> <td>change4</td> </tr> <tr> <td>5</td> <td>name5</td> <td>field5</td> <td>facility5</td> <td>change5</td> </tr> <tr> <td>6</td> <td>name6</td> <td>field6</td> <td>facility6</td> <td>change6</td> </tr> <tr> <td>7</td> <td>name7</td> <td>field7</td> <td>facility7</td> <td>change7</td> </tr> <tr> <td>8</td> <td>name8</td> <td>field8</td> <td>facility8</td> <td>change8</td> </tr> <tr> <td>9</td> <td>name9</td> <td>field9</td> <td>facility9</td> <td>change9</td> </tr> <tr> <td>10</td> <td>name10</td> <td>field10</td> <td>facility10</td> <td>change10</td> </tr> <tr> <td>11</td> <td>name11</td> <td>field11</td> <td>facility11</td> <td>change11</td> </tr> <tr> <td>12</td> <td>name12</td> <td>field12</td> <td>facility12</td> <td>change12</td> </tr> <tr> <td>13</td> <td>name13</td> <td>field13</td> <td>facility13</td> <td>change13</td> </tr> <tr> <td>14</td> <td>name14</td> <td>field14</td> <td>facility14</td> <td>change14</td> </tr> <tr> <td>15</td> <td>name15</td> <td>field15</td> <td>facility15</td> <td>change15</td> </tr> </tbody> </table> 作为axiac,但是,这是一种处理它的简单方法。

preg_replace_callback

<?php // SET OUR DEFAULTS $var["blue"]="ocean"; $var["red"]="fire"; $var["green"]="forest"; $string = 'people loves {{blue}} looks {{red}} and enjoy {{green}}'; // MAKE A CALLBACK FUNCTION THAT WILL REPLACE THE APPROPRIATE VALUES $callback_function = function($m) use ($var) { return $var[$m[2]]; }; // RUN THE PREG_REPLACE_CALLBACK - CALLING THE FUNCTION $string = preg_replace_callback('~(\{\{(.*?)\}\})~', $callback_function, $string); // DYNAMITE print $string; 附加到函数允许我们在我们的函数中使用use ($var)。所以我们需要做的就是使用它在大括号内匹配的部分作为数组的键。这样我们就可以获得键所在的数组的值,比如$var,并从函数中返回该值。

当我们运行它时,它输出以下内容:

blue

这是一个有效的演示:

http://ideone.com/2GzsoK