How to split a string with php

前端 未结 4 1632
野的像风
野的像风 2021-01-17 20:03

is there any nice way to split an string after \" \" or . ?

Like

$string = \"test.test\" result = test

$string = \"test doe\" result = test
<         


        
相关标签:
4条回答
  • 2021-01-17 20:29

    If you want to split on several different chars, take a look at preg_split

    //split string on space or period:
    $split=preg_split('/[ \.]/', $string);
    
    0 讨论(0)
  • 2021-01-17 20:37

    You want the strtok function. The manual gives this example:

    <?php
    $string = "This is\tan example\nstring";
    /* Use tab and newline as tokenizing characters as well  */
    $tok = strtok($string, " \n\t");
    
    while ($tok !== false) {
        echo "Word=$tok<br />";
        $tok = strtok(" \n\t");
    }
    ?>
    

    Though in your case I suspect that using explode twice is easier and looks better.

    0 讨论(0)
  • 2021-01-17 20:46

    You could do a strtr of . into space and then explode by space. Since strtr is very fast.

    0 讨论(0)
  • 2021-01-17 20:51

    There's string token strtok.

    0 讨论(0)
提交回复
热议问题