简体   繁体   English

PHP检测重复的单词

[英]PHP detect repeated word

Is it possible to detect a repeated word from this string : 是否可以从此字符串中检测到重复的单词:

$string = "dogdog";

I want a return of "str" only and not "strstr" 我只想返回“ str”而不是“ strstr”

I tried some good solution but it won't really do how i want it to be done : 我尝试了一些好的解决方案,但它并不能真正做到我希望的完成方式:

$string = "dogdog";
$length = strlen($string) / 2;
 echo substr($string, $length);

I want it to be detected from a big sentence ( $string = " dogdog something dogdog else bla bla, something, ( something ); something"; ).... String contains 2 repeated Correct words without separation , How is it to detect and fix it to a real single word? 我希望从一个大句子中检测出它($ string =“ dogdog something dogdog else bla blabla something,(something); something”;).... 字符串包含2个重复的正确单词,没有分隔 ,如何检测并修复成一个真正的单词?

explode / preg_split your sentence into an array of words, and then use your technique on each word. explode / preg_split你的句子成单词的阵列,然后使用技术在每个字。

You haven't specified what constitutes a "word"; 您尚未指定什么构成“单词”; this will determine precisely what delimiters you will need to explode on. 这将精确确定您需要使用哪些定界符。

You should do this iteratively. 您应该迭代执行此操作。 Here's a start: 这是一个开始:

//turn punctuation into something we can split on
$sanitized = preg_replace('/[^a-z0-9]+/i', '_', $string);
$words = explode("_",$sanitized);

//now that you have a 'clean' array, do logic
foreach($words as $word) {
    //tinker with word dup' logic here.
}

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

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