使用php,我有一个名称列表,我想在每行的第一行中添加它:
'http://
并将其添加到每行的最后一条:
' ,
示例>>
我有这个:
john
michel
hosein
ali
我想要这个:
'http://john' ,
'http://michel ' ,
'http://hosein' ,
'http://ali' ,
有什么代码可以为我做吗?
如果您的行在数组中,则可以使用类似的东西:
$out = array_map(function($item) {
return "'http://{$item}', ";
}, $data);
如果不是他们,您必须使用爆炸(或preg_split)将它们放入数组
我希望此代码会有所帮助:
<?
$lineStart= "'http://";
$lineEnd = "' , ";
$names = array("john", "michel", "hosein", "ali"); //array with names
for ($i=0; $i<count($names) ; $i++) //echo as many times as the number of names in a string
{
echo $lineStart.$names[$i].$lineEnd."<br>"; //just string concatenation
}
?>
例如,您的值位于名为$array
的数组中,您想将新格式保存在名为 $new_array
的数组中,您可以尝试这样做:
$new_array = array();
foreach($array as $value) {
$new_array[] = "'http://".$value."' ,";
}
<?php
$ary = array("john", "michel", "hosein", "ali");
$newArray = array();
foreach($ary as $val)
$newArray[] = "'http://" .$val. "', ";
?>
从文件中获取名称列表,
编辑:
<?php
$ary = file("inputfile.txt", FILE_IGNORE_NEW_LINES);
$newArray = array();
foreach($ary as $val) {
$newArray[] = "'http://".$val."', ";
}
?>
在这里, inputfile.txt 包含按行的名称。
ex。
john
michel
hosein
ali