我想根据起始和长度做多个子字符串替换。目前我有一个值为
的数组(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)
我想要的是
我目前的代码是
$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;
答案 0 :(得分:0)
我希望我能正确理解你的问题。如果是这样的话:让你的字符串准备好你要用花括号填写的点:
$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]);
(字符串/数组当然是一个例子,这取决于你的数据/愿望)
答案 1 :(得分:0)
$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;