【问题标题】:replace more than 2 different words within a string替换字符串中超过 2 个不同的单词
【发布时间】:2016-04-20 18:51:12
【问题描述】:

我有一个字符串包含我想用数组值替换的简码假设如果一个字符串中有 2 个不同的名称我想用匹配的数组值替换即array('pizza', 'fruit'); 用于替换为@987654322 @

所以[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] => [简码名称='generate'] [1] => [简码 id='2' name='corusel'] ) 你好世界,这是一个 lorem ipsum text corusel this 用于一些生成虚拟词

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

【问题讨论】:

  • 要替换的值是否在idname 中找到?
  • 值被名称替换
  • 那么[shortcode id='generate'] 什么都不做?

标签: php arrays regex replace


【解决方案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
?>

【讨论】:

  • 可以进行名称替换,但我希望整个短代码不仅要替换名称
  • 你提供的$2没看懂??
  • $2 保存从第二组捕获的值,在上述情况下,name='' 之间的所有值
  • 好的,所以在我的情况下它的数组('corusel','generate');或多于 2 所以代替 $2 ??你能清除这个吗
  • 你知道现在这个概念很棒而且对于 id 和 name 吗? [短代码名称='generate' id='2']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
相关资源
最近更新 更多