第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查一個單詞是單個單詞還是多個單詞并在 php 中潛入 2 個數(shù)組

如何檢查一個單詞是單個單詞還是多個單詞并在 php 中潛入 2 個數(shù)組

PHP
冉冉說 2023-03-04 16:58:53
我必須檢查一組字符串數(shù)據(jù)。例如,這是字符串:$keywords="today, tomorroy, world, is a good day , a good day is, today"我必須檢查是單詞還是多詞。如果是多個單詞,我必須訂購并只留下一個,例如:是美好的一天;美好的一天是我必須離開的唯一:是美好的一天。這句話我必須存儲在其他數(shù)組中。最后我必須只有這個結果:這是我的代碼:     $keywords = "";               $singleword="";     $multiword="";     foreach ($response->getResults() as $result) {                            $keywords .= $result->getText()->getValue() . ",";            if(count(explode(' ', $keywords)) > 1) {                $multiword++;                                                              }            $singleword++;                                         }          return $keywordsgenerated;我需要返回:今天,明天,世界,是美好的一天請你幫我解決,我是 php 的新手。
查看完整描述

1 回答

?
月關寶盒

TA貢獻1772條經(jīng)驗 獲得超5個贊

我想你正在尋找這樣的東西:


<?php

$keywords="today, tomorrow, world, is a good day , a good day is, today";

$arr = array_unique(explode(",",$keywords));


$words = [];

foreach($arr as $key=>$a) {    

    $words_explode = explode(" ", $a);

    foreach($words_explode as $w) {

        $words[] = $w;

    }        

}

$new_words = array_unique($words);


$result = implode(" ", $new_words);

foreach($arr as $item) {

    $result = str_replace($item,$item.",",$result);

}

$result = trim($result,", ");

var_dump($result);

更新:


我認為?我得到你要找的東西了嗎?


$keywords = "hansgrohe,hansgrohe focus,küchenarmatur,hansgrohe metris";

$arr = explode(',', $keywords);

$already = [];

$result = '';

foreach($arr as $word) {

    $subword = explode(" ", $word);

    foreach($subword as $actual_word) {

        if (!in_array($actual_word, $already)) {

            $result .= $actual_word . ' ';  

            $already[] = $actual_word;      

        }                

    }

}

$result = rtrim($result);


//would result in hansgrohe focus küchenarmatur metris

echo $result; 

更新2:


如果你想知道每個單詞出現(xiàn)的次數(shù)。


<?php

$keywords = "hansgrohe,hansgrohe focus,küchenarmatur,hansgrohe metris";

$arr = explode(",", $keywords);

$already = [];

$nrwords = [];

$result = '';

foreach($arr as $word) {

    $subword = explode(" ", $word);

    foreach($subword as $actual_word) {

        if (!in_array($actual_word, $already)) {

            $result .= $actual_word . ' ';  

            $already[] = $actual_word;    

            $nrwords[$actual_word] = 0;

        }                

        $nrwords[$actual_word]++;

    }

}

$result = rtrim($result);


//would result in hansgrohe focus küchenarmatur metris

echo $result; 


//would show how many of each word that exists

echo '<pre>';

print_r($nrwords);

echo '</pre>';


查看完整回答
反對 回復 2023-03-04
  • 1 回答
  • 0 關注
  • 141 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號