简体   繁体   English

PHP从字符串中获取前三个单词

[英]PHP Get first 3 words from string

The following code splits a string and selects all of the alphanumeric words containing minimum 3 letters. 下面的代码拆分一个字符串,并选择所有包含最少3个字母的字母数字单词。 However instead of getting all the words from the string, I want to get only first 3 valid words, but the problem is with the loop. 但是,我不想从字符串中获取所有单词,而是只获取前3个有效单词,但是问题出在循环上。 How can I stop the loop as soon as 3 valid words are found from the string. 从字符串中找到3个有效单词后,如何停止循环。

PS. PS。 If there is an alternative approach to the same thing, please suggest. 如果对同一件事有替代方法,请提出建议。 Thanks. 谢谢。

$string = "test test TEST test- -test _test blsdk.bldf,las";

$arr = preg_split('/[,\ \.;]/', $string);
$keywords = array_unique($arr);
foreach ($keywords as $keyword){
            if ((preg_match("/^[a-z0-9]/", $keyword) ) && (strlen($keyword) > 3)){
                    echo $keyword;
                    echo "<br />";
            }
        }

add a variable which counts up when a matching keyword is found. 添加一个变量,该变量在找到匹配的关键字时开始计数。 when counter reaches maximum then break the foreach loop 当计数器达到最大值时,中断foreach循环

$string = "test test TEST test- -test _test blsdk.bldf,las";

$arr = preg_split('/[,\ \.;]/', $string);
$keywords = array_unique($arr);
$i=0;
foreach ($keywords as $keyword){
            if ((preg_match("/^[a-z0-9]/", $keyword) ) && (strlen($keyword) > 3)){
                    echo $keyword;
                    echo "<br />";
                    $i++;
                    if ($i==3) break;
            }
        }

I think you need the 'break' keyword, try this (code not tested) - 我认为您需要使用'break'关键字,请尝试使用此代码(未经测试的代码)-

$string = "test test TEST test- -test _test blsdk.bldf,las";

$arr = preg_split('/[,\ \.;]/', $string);
$keywords = array_unique($arr);
$counter = 0;
foreach ($keywords as $keyword){
            if ((preg_match("/^[a-z0-9]/", $keyword) ) && (strlen($keyword) > 3)){
                    echo $keyword;
                    echo "<br />";
                    $counter = $counter + 1;
                    if ($counter == 3) break;
            }
        }

只是切断其余部分。

array_splice($arr, 3);

I would use a different approach As you use a regex to check your results maybe it is more clear, and maybe efficients, to use preg_match_all to do your work for you. 我会使用另一种方法,当您使用正则表达式检查结果时,使用preg_match_all为您完成工作可能更清楚,也可能效率更高。

<?php
$string = "test le  test TEST test- -test _test blsdk.bldf,las";

$arr=array();
preg_match_all('/\b([0-9A-Za-z]{3,})\b/', $string, $arr);
$keywords = array_slice(array_unique($arr[0]),0,3);
echo join('<br/>', $keywords);

In the question you state you want to select words with a minimum length of 3, but you test their length with > 3 . 在问题中,您要选择一个最小长度为3的单词,但要使用> 3测试它们的长度。 Change the regular expression accordingly if needed. 如果需要,相应地更改正则表达式。

Note: made the words unique as in the original test. 注意:使单词与原始测试相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM