替换字符串

时间:2016-01-15 08:05:22

标签: php arrays regex replace

我有一个包含短代码的字符串,我想用数组值替换,假设字符串中有2个不同的名称,我想用匹配的数组值替换,即array('pizza', 'fruit');用于替换为(Hello world lorem ipsum [name='apple'] lorem ipsum [name='bread'])

所以[name='apple']被水果取代 并将[name='bread']替换为披萨

在我当前的代码中,当放置按照放置值的顺序时,它正在完全替换,但当我切换放置在数组中的单词时,它按照数组序列的顺序替换

$content = "Hello world this is a lorem ipsum text [shortcode name='generate'] this is used for some [shortcode id='2' name='corusel'] dummy words";
preg_match_all("/\[(.*?)\]/", $content, $matches);

$values = array();

foreach($matches[1] as $match) {
    if(is_array($match)) {
        foreach($match as $value) {
            echo $values = str_replace($match, "[$match]", $match);
            $value2 = explode(" ", $match);

            echo "<br />";
        }
    } else {
        $values[] = str_replace($match, "[$match]", $match);
        $value2 = explode(" ", $match);
        $value3 = explode("=", $value2[1]);
        if(isset($value2[2])) {$value4 = explode("=", $value2[2]);}
        $val1   = str_replace("'", "", $value3[1]);
        if(isset($value4[1])) {$val2   = str_replace("'", "", $value4[1]);}
        if(isset($val2)){$val2;}
        echo "<br />";
    }
}

$text = array('corusel', 'generate');
print_r($values);
echo "<br />";
echo str_replace($values, $text, $content);

输出

  

数组([0] =&gt; [短代码名称=&#39;生成&#39;] [1] =&gt; [短代码ID =&#39; 2&#39;   name =&#39; corusel&#39;])Hello world这是一个lorem ipsum text corusel this   用于生成一些虚拟词

corusel正在替换生成这是错误的它应该替换corusel并且生成应该被生成当我切换位置时它完美地工作

1 个答案:

答案 0 :(得分:1)

如果可以在属性name中找到替代品(您的问题在此问题上不明确),您的目标可以非常轻松地实现(无需循环匹配并爆炸等):

<?php
$content = "Hello world this is a lorem ipsum text [shortcode name='generate'] this is used for some [shortcode id='2' name='corusel'] dummy words";
$regex = '~\[.*?name=([\'"])([^\'"]+)\1\]~';
# look for name=, followed by single or double quotes
# capture everything that is not a double/single quote into group2
# match the previously captured quotes
# the whole construct needs to be in square brackets

$content = preg_replace($regex, "$2", $content);
echo $content;
// output: Hello world this is a lorem ipsum text generate this is used for some corusel dummy words
?>

查看其他demo on regex101

要仅获取 name属性的值(即不替换任何内容),您可以在正则表达式定义后调用preg_match_all()

preg_match_all($regex, $content, $matches);
print_r($matches);
// your name values are in $matches[2]

您甚至可以进一步并将整个shortcode内容替换为您想要的任何内容:

<?php
$content = "Hello world this is a lorem ipsum text [shortcode name='generate'] this is used for some [shortcode id='2' name='carousel'] dummy words";
$regex = '~\[.*?name=([\'"])([^\'"]+)\1\]~';

$replacements = array("generate" => "Some generator stuff here", "carousel" => "Rollercoaster");   

$content = preg_replace_callback($regex, 
    function ($match) {
        global $replacements;
        $key = $match[2];
        return $replacements[$key];
    },
    $content
    );
echo $content;

// output: Hello world this is a lorem ipsum text Some generator stuff here this is used for some Rollercoaster dummy words
?>