PHP - remove duplicate syllable word

馋奶兔 提交于 2019-12-10 16:34:19

问题


i'm new and wanna know how to using regex in php. if i have text like "haha" "abcabcabc" "hehehe" "cupcupcupcup" (contain duplicate of syllable word).

How to remove that text?

(say $text="cupcupcup";", how to make this like $text="";)

sorry if there is same ask (i have search, but cant find page that describe my issue)

thanks for your appreciate. :)


回答1:


Try

$txt = preg_replace("/^(.*)(\\1+)$/", "", $txt);

This searches for a sequence at the start of the string, then matches at least one repetition of that string, then matches the end of the string.

e.g.

$txt = preg_replace("/^(.*)(\\1+)$/", "", "cupcupcup");  //=> ""
$txt = preg_replace("/^(.*)(\\1+)$/", "", "cucupcup");   //=> "cucupcup"



回答2:


Well I guess this would do

\b(?=\w*[aeiou]\w*)(\w*)\1+
     -------------
           |
           |->check if its a syllable word

replace it with ""




回答3:


So here's what I came up with:

([b-df-hj-np-tv-xz][aeiouy](?:[a-z])?|[aeiouy][b-df-hj-np-tv-xz](?:[a-z])?)(\1){2,}

Online demo

Explanation:

  • [b-df-hj-np-tv-xz] : Match consonants.
  • [aeiouy] : Match vowels.
  • [a-z] : Match a letter.
  • ([b-df-hj-np-tv-xz][aeiouy](?:[a-z])? : Match consonant followed by a vowel and optionally followed by a letter.
  • |[aeiouy][b-df-hj-np-tv-xz](?:[a-z])?) : Or match a vowel followed by a consonant followed optionally by a letter.
  • (\1){2,} : Repeat group 1 two or more times.



回答4:


if the text like :

$txt = preg_replace("/(.*)(\\1+)/", "", "the text is  cupcupcup and cupcupcupa");

that will remove cupcupcup and cupcupcupa . how to just remove cupcupcup ?



来源:https://stackoverflow.com/questions/16884258/php-remove-duplicate-syllable-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!