一个字符串内更换2个以上不同的字 [英] replace more than 2 different words within a string

查看:115
本文介绍了一个字符串内更换2个以上不同的字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串包含短codeS,我想被替换为数组值让我们假设,如果有一个字符串中的2个不同的名字,我想再配数组值即阵列('比萨','水果'); 是与(世界您好Lorem存有[名称='苹果'] Lorem存有[名称='面包'] )

I have a string contains shortcodes that I would like to be replaced with array values let suppose if there are 2 different names within a string that I would like to replace with matched array values i.e array('pizza', 'fruit'); is for replacing with (Hello world lorem ipsum [name='apple'] lorem ipsum [name='bread'])

所以 [名称='苹果'] 来用水果来代替
[名称='面包'] 与比萨饼替换

so [name='apple'] to be replaced with fruit and [name='bread'] to be replaced with pizza

在我目前的code这是完全更换时的摆放在放置值的顺序,但是当我切换词语的阵列放置在阵列序列顺序替换

Within my current code it is replacing perfectly when the placing is in order of placed values but when i switch the words placing in array it replacing in the order of array sequence

$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] => [短code名称='生成'] [1] => [短code ID ='2'
  名称= corusel'])世界,你好,这是一个Lorem存有文字corusel这
  用于一些生成虚设字

Array ( [0] => [shortcode name='generate'] [1] => [shortcode id='2' name='corusel'] ) Hello world this is a lorem ipsum text corusel this is used for some generate dummy words

corusel正在取代生成这是不对的,应该更换corusel并生成应当我切换位置生成它完美的作品来代替

corusel is replacing generate this is wrong it should replace corusel and generate should be replaced by generate when i switch position it works perfectly

推荐答案

如果可以在属性的名字可以找到替代(你的问题不明确就此事) ,你的目标一定能够达到pretty容易(无需遍历所有的比赛和爆炸等):

If the substitute can be found in the attribute name (your question is not clear on the matter), your goal can be achieved pretty easily (no need to loop over the matches and explode, etc.):

<?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
?>

查看regex101 额外演示。

See an additional demo on regex101.

要只获取的值名称属性(即不更换任何东西),你可以调用 preg_match_all()正则表达式定义后:

To only get the values (i.e. not replacing anything yet) of the name attribute, you can call preg_match_all() after the regex definition:

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

您甚至可以去的更多并更换整个短code 啄任何你想要的:

You can even go further and replace the whole shortcode thingy with anything you want:

<?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个以上不同的字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆