dongzhi4498 2017-08-21 05:37
浏览 14
已采纳

在数组php中拆分和存储字符串

I have a string

$descr = "Hello this is a test string";

What I am trying to do is to split the string and store each word which is separated using space into separate array index in PHP. Should I use

$myarray = preg_split("[\s]",$descr);

Expected outcome :

$myarray(1) : hello
$myarray(2) : this
$myarray(3) : is
$myarray(4) : a
$myarray(5) : test
$myarray(6) : string

Each number denotes array index

  • 写回答

4条回答 默认 最新

  • douying3251 2017-08-21 05:45
    关注

    You need to use explode() like below:-

    $myarray = explode(' ', $descr);
    print_r($myarray);
    

    Output:-https://eval.in/847916

    To re-index and lowercase each word in your array do like this:-

    <?php
    
    $descr = "Hello this is a test string";
    
    $myarray = explode(' ', $descr);
    
    $myarray = array_map('strtolower',array_combine(range(1, count($myarray)), array_values($myarray)));
    print_r($myarray);
    

    Output:-https://eval.in/847960

    To get how many elements are there in the array:-

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备