在每个 php 实例后附加一个数字


Append a number to every php instance

我的网站上有一个表单,用户可以在其中向多个收件人发送消息,他们输入用逗号分隔的收件人号码,我在 php 脚本中将数字作为单个 post 变量接收,但我需要在每个数字前面添加收件人的区号,我试图使用逗号作为分隔符来分解变量,但我不知道从那里做什么

$recipient=$_POST['recipient'];
$numbers=explode(',' $recipient);
for ($i = 0; $i <=sizeof($numbers); $i++) {
}
我不知道

您如何在脚本中获取区号,但是要在号码前面加上区号,您只需使用字符串串联,如下所示:

$areacode = 123; // this is received dynamically
$result = array(); // initialize empty array
for ($i = 0; $i <= sizeof($numbers); $i++) {
    $result[] = $areacode . '_' . $numbers[$i];
}

这也可以使用foreach来完成:

foreach (explode(',', $recipient) as $number) {
    $result[] = $areacode . '_' . $number;
}

现在,如果你想把它作为逗号分隔的字符串找回去,你可以使用 implode() ,如下所示:

$result_string = implode(',', $result);

简短的回答是这样的:

$numbers_with_area_codes = Array();
for ($i = 0; $i <= sizeof($numbers); $i++) {
  $numbers_with_area_codes[] = '+372'.$numbers[$i];
}