如何同时替换多个子字符串

how to do multiple sub string replace at same time

我想根据起始和长度进行多个子字符串替换。目前我有一个数组

(user-id,replace-name,starting,length)

my string: "Hi Rameez plz call charlie"

sample array : array('123','Rameez Rami',4,6),  array('124','Charlie Russet',20,7)

而我想要的是

拉米兹 Rameez 查理 Charlie

我当前的代码是

$linkAddedDescription=Input::get('description');
            foreach ($descriptionMapArray as $key=> $taggedItem) {
            //print_r($taggedItem);die;
                $tagWordStarting=$taggedItem[0];
                $tagWordLength=$taggedItem[1];
                $taggedItemData=$taggedItem[3];
                $descriptionTagLink='<a href="'.URL::to("/").'/user/profile/'.$taggedItemData->id.'">'.$taggedItemData->name.'</a>';

                $linkAddedDescription=substr_replace($linkAddedDescription,$descriptionTagLink,$tagWordStarting,$tagWordLength); 
              //break;
            }
            print_r($linkAddedDescription);die;   

希望我能正确理解您的问题。如果是这样:准备好字符串,并在花括号中填写要填充的位置:

$outputString = 'Hi {name} plz call {othername}';
$firstPerson = array('123', 'Rameez', 4, 6);
$secondPerson = array('132', 'Charlie', 3, 8);

然后使用 str_replace 用您的数据填充它们:

$outputString = str_replace('{name}', $firstPerson[1]);
$outputString = str_replace('{othername}', $secondPerson[1]);

(当然string/arrays只是一个例子,这个要看你的data/wishes)

$descriptionMapArray=json_decode(Input::get('ideaTagDescriptionMap'));
        $str=Input::get('description');
        $startofsub=0;
        $taggedDescription='';
        foreach ($descriptionMapArray as $key=> $taggedItem) {
            $tagWordStarting=$taggedItem[0];
            $tagWordLength=$taggedItem[1];
            $taggedItemData=$taggedItem[3];
            $descriptionTagLink='<a href="/user/profile/'.$taggedItemData->id.'">'.$taggedItemData->name.'</a>';

            $tolen=($tagWordStarting+$tagWordLength);
            $to=$tolen-$tagWordStarting;
            $v=$tolen-$startofsub;
            $sbstr=substr($str,$startofsub,$v);
            $sbstr=substr_replace($sbstr,$descriptionTagLink,($tagWordStarting-$startofsub),$tagWordLength);
            $taggedDescription.=$sbstr;
            $startofsub=$tagWordStarting+$tagWordLength;
        }    
        if($startofsub < strlen(Input::get('description'))){
            $lst_part=strlen(Input::get('description'))-$startofsub;
            $taggedDescription.=substr($str,$startofsub,$lst_part);
        }
        echo '<br/>RESULT:-'.$taggedDescription;
        die;