用数组值替换分隔符之间每次出现的文本(在php中)
我要替换两次之间的每一次事件用数组值替换分隔符之间每次出现的文本(在php中),php,regex,preg-replace,Php,Regex,Preg Replace,我要替换两次之间的每一次事件 {{ 及 我想用符号{{}内的旧文本来选择新的, 我有一个数组: $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_替换,但没有结果
{{
及
我想用符号{{}内的旧文本来选择新的,
我有一个数组:
$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_替换,但没有结果:
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);
}
尝试:
尝试:
这是PHP函数的完美示例:
这是PHP函数的完美示例:
我也会像axiac一样使用
preg\u replace\u 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;
下面是一个工作演示:
我也会像axiac一样使用
preg\u replace\u 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;
下面是一个工作演示:
你能告诉我们你试过什么吗?当然:函数替换标签($startPoint,$endPoint,$newText,$source){$var[“blue”]=“ocean”;$var[“red”]=“fire”;$var[“green”]=“forest”;return preg_replace('.'.''preg_quote($startPoint.))('.*)('.preg"endPoint.'.'.'.'.'')\si',idon-KNOW-KNOW-what-PUT-HERE,$source)}将此添加到question@Thamizhan我刚做了,你能告诉我们你试过什么吗?当然:函数替换标签($startPoint,$endPoint,$newText,$source){$var[“blue”]=“ocean”;$var[“red”]=“fire”;$var[“green”]=“forest”;$var[“green”]=“forest”;return preg_replace('.''('..preg_quote($startPoint.))(.*)(.*)('.preg).$endPoint.)#si','IDONT-KNOW-WHAT-PUT-HERE',$source);}将此添加到question@Thamizhan我刚做过,数组$var[]来自一个数据库,非常长,我只想替换文本显示的单词数组$var[]来自一个数据库,非常长,我只想替换文本显示的单词
$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
);
}
<?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;
people loves ocean looks fire and enjoy forest