douba9425 2016-05-21 12:49 采纳率: 100%
浏览 11
已采纳

PHP破碎/爆炸文本

I have a one input that is used for a "Name, Surname and Family name". But I want if user enter more than 3 names, all after 3(4,5,6...) to be like family. But if you submit 2 names the Family name must be "0".

Here is my code:

<form method="POST" action="getNames.php">
    Names: <input type="text" name="names" />
    <input type="submit" name="submit" />
</form>

<?php

$names = $_POST['names'];

// some action here to get names

$firstName = ""; // Get first name
$surName = ""; // Get Sur name
$familyName = ""; // Get family name

?>

I will be very glad if someone help me. Thanks!

  • 写回答

3条回答 默认 最新

  • douqin7086 2016-05-21 13:04
    关注

    This code will output an array of familly, if an input is missing, it will be replaced with a 0.

    PHP

    <?php
    $_POST['names'] = 'sofiene, totose, tata, titi';
    $names = explode(',', $_POST['names']);
    $familly = [];
    
    for ($i = 0; $i < count($names); $i+=3) {
        $familly[$i] = [
            'firstName' => (isset($names[$i])) ? $names[$i] : 0,
            'surName' => (isset($names[$i + 1])) ? $names[$i + 1] : 0,
            'familyName' => (isset($names[$i + 2])) ? $names[$i + 2] : 0
        ];
    }
    var_dump($familly);
    ?>
    

    EvalIn

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看