[英]How to replace a Nth occurrence in a string (PHP)
说,有一个数组:
array:4 [
0 => "{__}"
1 => "{one|two|three}"
2 => "{__}"
3 => "{red|green|blue}"
]
和文字:
> Lorem {__} dolor sit amet, consectetur {one|two|three} elit, sed do
> eiusmod tempor incididunt ut labore et dolore {__} aliqua. Ut enim ad
> minim veniam, quis nostrud exercitation ullamco laboris nisi ut
> aliquip ex ea commodo {one|two|three}. Duis {red|green|blue} irure
> dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
> nulla pariatur.
如何将所有{__}替换为<input type="text" name="text[n]" />
(n-条目数),并将所有{a | b | c | ... | N}替换为
<select name="list[n]">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="4">...</option>
<option value="N">N</option>
</select>
(n-参赛人数)
你的算法应该是这样的
1-从输入中读取字符串
2- $ Step2arr =使用'{__}'中断数组
3-运行第一步中获得的数组的foreach
$newstr = '' ;
foreach($Step2arr as $key=>$val){
if( count($Step2arr) - 1 < $key )
$newstr = $newstr.$val.'<input type="text" name="text['.$key.']" /> ' ;
}
4-现在,新字符串具有$ newstr字符串,例如“ Lorem
现在,您也可以对第二个{a | b | c}字符串重复相同的过程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.