Extract words from string with preg_match_all

前端 未结 7 2140
你的背包
你的背包 2020-12-21 17:14

I\'m not good with regex but i want to use it to extract words from a string.

The words i need should have minimum 4 characters and the provided string can

7条回答
  •  隐瞒了意图╮
    2020-12-21 18:01

    Explode your string with spaces (which will create an array with all words), then check if the word is bigger than 4 letters.

    //The string you want to explode
    $string = "Sus azahares presentan gruesos pétalos blancos teñidos de rosa o violáceo en la parte externa, con numerosos estambres."
    //explode your $string, which will create an array which we will call $words
    $words = explode(' ', $string);
    
    //for each $word in $words
    foreach($words as $word)
    {
        //check if $word length if larger then 4
        if(strlen($word) > 4)
        {
            //echo the $word
            echo $word;
        }
    }
    

    strlen();

    strlen — Get string length

    explode();

    explode — Split a string by string

提交回复
热议问题