복붙노트

문자열에 특정 단어가 포함되어 있는지 어떻게 확인합니까?

PHP

문자열에 특정 단어가 포함되어 있는지 어떻게 확인합니까?

중히 여기다:

$a = 'How are you?';

if ($a contains 'are')
    echo 'true';

위 코드를 가지고 있다고 가정하면 ($ a에 'are'가 포함 된 경우) 문장을 작성하는 올바른 방법은 무엇입니까?

해결법

  1. ==============================

    1.

    strpos () 함수를 사용할 수 있습니다. strpos () 함수는 하나의 문자열이 다른 문자열에 포함되어 있는지를 찾는 데 사용됩니다.

    $a = 'How are you?';
    
    if (strpos($a, 'are') !== false) {
        echo 'true';
    }
    

    ! == false의 사용은 고의적입니다. strpos ()는 haystack 문자열에서 바늘 문자열이 시작되는 오프셋을 반환하거나 needle이 발견되지 않으면 false를 반환합니다. 0은 유효한 오프셋이고 0은 "거짓"이므로! strpos ($ a, 'are')와 같은 간단한 구문을 사용할 수 없습니다.

  2. ==============================

    2.

    정규 표현식을 사용할 수 있습니다. 다른 사용자가 언급 한 것처럼 strpos에 비해 단어 일치가 더 좋습니다. 운임, 치료, 응시 등과 같은 문자열에도 true를 반환합니다. 정규 표현식에서는 단어 경계를 사용하여 피할 수 있습니다.

    간단한 일치는 다음과 같이 보일 수 있습니다.

    $a = 'How are you?';
    
    if (preg_match('/\bare\b/',$a))
        echo 'true';
    

    성능면에서 strpos는 약 3 배 빨라졌고 마음 속에서 한 번에 100 만회를 비교했을 때 1.5 초로 경기가 끝났고 strpos에는 0.5 초가 걸렸습니다.

  3. ==============================

    3.

    다음과 같은 상황에서 유용 할 수있는 작은 유틸리티 함수가 있습니다.

    // returns true if $needle is a substring of $haystack
    function contains($needle, $haystack)
    {
        return strpos($haystack, $needle) !== false;
    }
    
  4. ==============================

    4.

    이 답변의 대부분은 문자열에 하위 문자열이 표시되는지 알려주지 만 일반적으로 하위 단어가 아닌 특정 단어를 찾는 경우 원하는 것이 아닙니다.

    차이점이 뭐야? 하위 문자열은 다른 단어 안에 나타날 수 있습니다.

    이것을 줄이는 한 가지 방법은 단어 경계 (\ b)와 결합 된 정규 표현식을 사용하는 것입니다.

    function containsWord($str, $word)
    {
        return !!preg_match('#\\b' . preg_quote($word, '#') . '\\b#i', $str);
    }
    

    이 방법은 위에서 언급 한 것과 동일한 오 탐지 (false positives)를 가지고 있지는 않지만, 자체적 인 몇 가지 단점이 있습니다. 단어 경계는 단어가 아닌 문자 (\ W)와 일치합니다.이 문자는 -z, A-Z, 0-9 또는 _이 아닌 문자가됩니다. 즉, 숫자와 밑줄은 단어 문자로 계산 될 것이고이 같은 시나리오는 실패합니다.

    이보다 더 정확한 것을 원한다면 영어 구문 구문 분석을 시작해야합니다. 이것은 꽤 큰 웜입니다. (어쨌든, 항상 주어진 것은 아니지만) 구문의 적절한 사용을 가정합니다.

  5. ==============================

    5.

    문자열에 다른 문자열이 포함되어 있는지 확인하려면 PHP 함수 strpos ()를 사용할 수 있습니다.

    int strpos (문자열 $ haystack, mixed $ needle [, int $ offset = 0])

    <?php
    
    $haystack = 'how are you';
    $needle = 'are';
    
    if (strpos($haystack,$needle) !== false) {
        echo "$haystack contains $needle";
    }
    
    ?>
    

    주의:

    찾고있는 바늘이 건초 더미의 시작 부분에 있다면 0을 반환합니다. == 비교하면 효과가 없을 것입니다. ===

    A == 부호는 비교이며 왼쪽 변수 / 표현식 / 상수가 오른쪽 / 변수 / 표현식 / 상수와 동일한 값을 갖는지 여부를 테스트합니다.

    === 부호는 두 변수 / 표현식 / 상수가 동일하고 유형이 같은지 확인하기위한 비교입니다. 즉, 둘 다 문자열이거나 둘 다 정수입니다.

  6. ==============================

    6.

    대 / 소문자를 구분하지 않으면 strstr () 또는 stristr ()을 사용하는 것이 다른 옵션입니다.

  7. ==============================

    7.

    strpos ()를 보자.

    <?php
        $mystring = 'abc';
        $findme   = 'a';
        $pos = strpos($mystring, $findme);
    
        // Note our use of ===. Simply, == would not work as expected
        // because the position of 'a' was the 0th (first) character.
        if ($pos === false) {
            echo "The string '$findme' was not found in the string '$mystring'.";
        }
        else {
            echo "The string '$findme' was found in the string '$mystring',";
            echo " and exists at position $pos.";
        }
    ?>
    
  8. ==============================

    8.

    stripos ()를 사용하여 대소 문자를 구분하지 않는 매칭을 사용합니다.

    if (stripos($string,$stringToSearch) !== false) {
        echo 'true';
    }
    
  9. ==============================

    9.

    "거짓"및 "진실"문제를 피하려면 substr_count를 사용할 수 있습니다.

    if (substr_count($a, 'are') > 0) {
        echo "at least one 'are' is present!";
    }
    

    strpos보다 약간 느리지 만 비교 문제는 피할 수 있습니다.

  10. ==============================

    10.

    Peer to Sam Goody와 Lego Stormtrooper의 의견.

    여러 단어의 근접성 / 관련성을 기준으로 검색 결과의 순위를 매기는 PHP 알고리즘을 찾고있는 경우 다음은 PHP를 사용하여 검색 결과를 생성하는 빠르고 쉬운 방법입니다.

    strpos (), preg_match (), strstr () 또는 stristr ()과 같은 다른 부울 검색 방법의 문제점

    벡터 공간 모델과 tf-idf (용어 주파수 - 문서 빈도 용어)에 기반한 PHP 방법 :

    어렵다고 들리지만 놀랍게도 쉽습니다.

    문자열에서 여러 단어를 검색하려는 경우 중대한 문제는 각 문자열에 하나의 가중치를 지정하는 것입니다.

    문자열을 전체 문자열과 같이 어떻게 표현하는지에 따라 문자열에 용어를 가중시킬 수 있다면, 우리는 질의와 가장 일치하는 결과로 결과를 정렬 할 수 있습니다.

    이것은 SQL 전체 텍스트 검색의 작동 방식에서 그리 멀지 않은 벡터 공간 모델의 아이디어입니다.

    function get_corpus_index($corpus = array(), $separator=' ') {
    
        $dictionary = array();
    
        $doc_count = array();
    
        foreach($corpus as $doc_id => $doc) {
    
            $terms = explode($separator, $doc);
    
            $doc_count[$doc_id] = count($terms);
    
            // tf–idf, short for term frequency–inverse document frequency, 
            // according to wikipedia is a numerical statistic that is intended to reflect 
            // how important a word is to a document in a corpus
    
            foreach($terms as $term) {
    
                if(!isset($dictionary[$term])) {
    
                    $dictionary[$term] = array('document_frequency' => 0, 'postings' => array());
                }
                if(!isset($dictionary[$term]['postings'][$doc_id])) {
    
                    $dictionary[$term]['document_frequency']++;
    
                    $dictionary[$term]['postings'][$doc_id] = array('term_frequency' => 0);
                }
    
                $dictionary[$term]['postings'][$doc_id]['term_frequency']++;
            }
    
            //from http://phpir.com/simple-search-the-vector-space-model/
    
        }
    
        return array('doc_count' => $doc_count, 'dictionary' => $dictionary);
    }
    
    function get_similar_documents($query='', $corpus=array(), $separator=' '){
    
        $similar_documents=array();
    
        if($query!=''&&!empty($corpus)){
    
            $words=explode($separator,$query);
    
            $corpus=get_corpus_index($corpus, $separator);
    
            $doc_count=count($corpus['doc_count']);
    
            foreach($words as $word) {
    
                if(isset($corpus['dictionary'][$word])){
    
                    $entry = $corpus['dictionary'][$word];
    
    
                    foreach($entry['postings'] as $doc_id => $posting) {
    
                        //get term frequency–inverse document frequency
                        $score=$posting['term_frequency'] * log($doc_count + 1 / $entry['document_frequency'] + 1, 2);
    
                        if(isset($similar_documents[$doc_id])){
    
                            $similar_documents[$doc_id]+=$score;
    
                        }
                        else{
    
                            $similar_documents[$doc_id]=$score;
    
                        }
                    }
                }
            }
    
            // length normalise
            foreach($similar_documents as $doc_id => $score) {
    
                $similar_documents[$doc_id] = $score/$corpus['doc_count'][$doc_id];
    
            }
    
            // sort from  high to low
    
            arsort($similar_documents);
    
        }   
    
        return $similar_documents;
    }
    

    사례 1

    $query = 'are';
    
    $corpus = array(
        1 => 'How are you?',
    );
    
    $match_results=get_similar_documents($query,$corpus);
    echo '<pre>';
        print_r($match_results);
    echo '</pre>';
    

    결과

    Array
    (
        [1] => 0.52832083357372
    )
    

    사례 2

    $query = 'are';
    
    $corpus = array(
        1 => 'how are you today?',
        2 => 'how do you do',
        3 => 'here you are! how are you? Are we done yet?'
    );
    
    $match_results=get_similar_documents($query,$corpus);
    echo '<pre>';
        print_r($match_results);
    echo '</pre>';
    

    결과

    Array
    (
        [1] => 0.54248125036058
        [3] => 0.21699250014423
    )
    

    사례 3

    $query = 'we are done';
    
    $corpus = array(
        1 => 'how are you today?',
        2 => 'how do you do',
        3 => 'here you are! how are you? Are we done yet?'
    );
    
    $match_results=get_similar_documents($query,$corpus);
    echo '<pre>';
        print_r($match_results);
    echo '</pre>';
    

    결과

    Array
    (
        [3] => 0.6813781191217
        [1] => 0.54248125036058
    )
    

    많은 개선이 있어야한다. 이 모델은 자연스러운 쿼리에서 좋은 결과를 얻는 방법을 제공하지만, strpos (), preg_match (), strstr () 또는 stristr ()과 같은 부울 연산자가없는.

    참고 사항

    선택적으로 단어를 검색하기 전에 중복을 제거하십시오.

    1. 정규화

    2. 스톱 워드 제거

    3. 사전 대체

    자원

  11. ==============================

    11.

    또 다른 옵션은 strstr () 함수를 사용하는 것입니다. 같은 것 :

    if (strlen(strstr($haystack,$needle))>0) {
    // Needle Found
    }
    

    주의 할 점 : strstr () 함수는 대소 문자를 구별합니다. 대 / 소문자를 구분하지 않고 검색하려면 stristr () 함수를 사용하십시오.

  12. ==============================

    12.

    strpos, strstr 및 이와 유사한 함수를 사용한 응답 중 멀티 바이트 문자열 함수 (2015-05-08)가 아직 언급되지 않았다는 사실에 조금 감탄했습니다.

    기본적으로 독일어, 프랑스어, 포르투갈어, 스페인어 등 일부 언어의 문자 (예 : ä, é, ô, ç, º, ñ)로 단어를 찾는 데 어려움이있는 경우 앞에 함수는 mb_. 따라서 허용 된 대답 대신 mb_strpos 또는 mb_stripos (대소 문자를 구분하지 않는 일치)를 사용합니다.

    if (mb_strpos($a,'are') !== false) {
        echo 'true';
    }
    

    모든 데이터가 UTF-8로 100 % 보장되지 않는다면, mb_ 함수를 사용하는 것이 좋습니다.

    왜 절대적으로 모든 소프트웨어 개발자가 절대적으로, 절대적으로 유니 코드와 문자 집합에 대해 알아야한다는 것을 이해할 수있는 좋은 기사입니다 (변명의 여지가 없습니다!). Joel Spolsky.

  13. ==============================

    13.

    아래의 함수도 작동하며 다른 함수에 종속되지 않습니다. 네이티브 PHP 문자열 조작 만 사용합니다. 개인적으로는 권장하지 않지만 작동 원리는 다음과 같습니다.

    <?php
    
    if (!function_exists('is_str_contain')) {
      function is_str_contain($string, $keyword)
      {
        if (empty($string) || empty($keyword)) return false;
        $keyword_first_char = $keyword[0];
        $keyword_length = strlen($keyword);
        $string_length = strlen($string);
    
        // case 1
        if ($string_length < $keyword_length) return false;
    
        // case 2
        if ($string_length == $keyword_length) {
          if ($string == $keyword) return true;
          else return false;
        }
    
        // case 3
        if ($keyword_length == 1) {
          for ($i = 0; $i < $string_length; $i++) {
    
            // Check if keyword's first char == string's first char
            if ($keyword_first_char == $string[$i]) {
              return true;
            }
          }
        }
    
        // case 4
        if ($keyword_length > 1) {
          for ($i = 0; $i < $string_length; $i++) {
            /*
            the remaining part of the string is equal or greater than the keyword
            */
            if (($string_length + 1 - $i) >= $keyword_length) {
    
              // Check if keyword's first char == string's first char
              if ($keyword_first_char == $string[$i]) {
                $match = 1;
                for ($j = 1; $j < $keyword_length; $j++) {
                  if (($i + $j < $string_length) && $keyword[$j] == $string[$i + $j]) {
                    $match++;
                  }
                  else {
                    return false;
                  }
                }
    
                if ($match == $keyword_length) {
                  return true;
                }
    
                // end if first match found
              }
    
              // end if remaining part
            }
            else {
              return false;
            }
    
            // end for loop
          }
    
          // end case4
        }
    
        return false;
      }
    }
    

    테스트:

    var_dump(is_str_contain("test", "t")); //true
    var_dump(is_str_contain("test", "")); //false
    var_dump(is_str_contain("test", "test")); //true
    var_dump(is_str_contain("test", "testa")); //flase
    var_dump(is_str_contain("a----z", "a")); //true
    var_dump(is_str_contain("a----z", "z")); //true 
    var_dump(is_str_contain("mystringss", "strings")); //true 
    
  14. ==============================

    14.

    if (preg_match('are', $a)) {
       echo 'true';
    }
    
  15. ==============================

    15.

    나는 이것에 약간의 문제가 있었고 마침내 나는 내 자신의 해결책을 창조하기로 결정했다. 정규 표현식 엔진을 사용하지 않고 :

    function contains($text, $word)
    {
        $found = false;
        $spaceArray = explode(' ', $text);
    
        $nonBreakingSpaceArray = explode(chr(160), $text);
    
        if (in_array($word, $spaceArray) ||
            in_array($word, $nonBreakingSpaceArray)
           ) {
    
            $found = true;
        }
        return $found;
     }
    

    이전 솔루션은 다른 단어의 접두사로 사용되는 단어에 대한 대답이 아님을 알 수 있습니다. 예제를 사용하려면 다음을 수행하십시오.

    $a = 'How are you?';
    $b = "a skirt that flares from the waist";
    $c = "are";
    

    위의 예제에서 $ a와 $ b는 모두 $ c를 포함하지만, $ a에만 $ c를 포함한다고 말할 수 있습니다.

  16. ==============================

    16.

    strstr 함수를 사용할 수 있습니다.

    $haystack = "I know programming";
    $needle   = "know";
    $flag = strstr($haystack, $needle);
    
    if ($flag){
    
        echo "true";
    }
    

    inbuilt 함수를 사용하지 않고 :

    $haystack  = "hello world";
    $needle = "llo";
    
    $i = $j = 0;
    
    while (isset($needle[$i])) {
        while (isset($haystack[$j]) && ($needle[$i] != $haystack[$j])) {
            $j++;
            $i = 0;
        }
        if (!isset($haystack[$j])) {
            break;
        }
        $i++;
        $j++;
    
    }
    if (!isset($needle[$i])) {
        echo "YES";
    }
    else{
        echo "NO ";
    }
    
  17. ==============================

    17.

    PHP에서 문자열에 특정 하위 문자열이 포함되어 있는지 확인하는 가장 좋은 방법은 다음과 같은 간단한 도우미 함수를 사용하는 것입니다.

    function contains($haystack, $needle, $caseSensitive = false) {
        return $caseSensitive ?
                (strpos($haystack, $needle) === FALSE ? FALSE : TRUE):
                (stripos($haystack, $needle) === FALSE ? FALSE : TRUE);
    }
    
    var_dump(contains('bare','are'));            // Outputs: bool(true)
    var_dump(contains('stare', 'are'));          // Outputs: bool(true)
    var_dump(contains('stare', 'Are'));          // Outputs: bool(true)
    var_dump(contains('stare', 'Are', true));    // Outputs: bool(false)
    var_dump(contains('hair', 'are'));           // Outputs: bool(false)
    var_dump(contains('aren\'t', 'are'));        // Outputs: bool(true)
    var_dump(contains('Aren\'t', 'are'));        // Outputs: bool(true)
    var_dump(contains('Aren\'t', 'are', true));  // Outputs: bool(false)
    var_dump(contains('aren\'t', 'Are'));        // Outputs: bool(true)
    var_dump(contains('aren\'t', 'Are', true));  // Outputs: bool(false)
    var_dump(contains('broad', 'are'));          // Outputs: bool(false)
    var_dump(contains('border', 'are'));         // Outputs: bool(false)
    
  18. ==============================

    18.

    짧은 버전

    $result = false!==strpos($a, 'are');
    
  19. ==============================

    19.

    '단어'를 찾으려면 실제로 다른 단어의 일부가 될 수있는 일련의 문자가 발생하는 대신 다음을 수행하는 것이 좋은 해결책이 될 수 있습니다.

    $string = 'How are you?';
    $array = explode(" ", $string);
    
    if (in_array('are', $array) ) {
        echo 'Found the word';
    }
    
  20. ==============================

    20.

    strstr () 및 stristr ()을 사용하여 문자열에서 단어의 발생을 찾는 또 다른 옵션은 다음과 같습니다.

    <?php
        $a = 'How are you?';
        if (strstr($a,'are'))  // Case sensitive
            echo 'true';
        if (stristr($a,'are'))  // Case insensitive
            echo 'true';
    ?>
    
  21. ==============================

    21.

    대소 문자를 구분하지 않는 형식을 사용해야합니다. 입력 된 값이 작거나 대문자로 되어도 문제가되지 않습니다.

    <?php
    $grass = "This is pratik joshi";
    $needle = "pratik";
    if (stripos($grass,$needle) !== false) { 
    
     /*If i EXCLUDE : !== false then if string is found at 0th location, 
       still it will say STRING NOT FOUND as it will return '0' and it      
       will goto else and will say NOT Found though it is found at 0th location.*/
        echo 'Contains word';
    }else{
        echo "does NOT contain word";
    }
    ?>
    

    여기서 stripos는 case를 고려하지 않고 heystack에서 바늘을 찾습니다 (small / caps).

    출력이있는 PHP 코드 샘플

  22. ==============================

    22.

    세 가지 방법으로 수행 할 수 있습니다.

     $a = 'How are you?';
    

    1 stristr ()

     if (strlen(stristr($a,"are"))>0) {
        echo "true"; // are Found
     } 
    

    2-strpos ()

     if (strpos($a, "are") !== false) {
       echo "true"; // are Found
     }
    

    3- preg_match ()

     if( preg_match("are",$a) === 1) {
       echo "true"; // are Found
     }
    
  23. ==============================

    23.

    substr_count를 사용하는 많은 답변은 결과가> 0인지 확인합니다. 그러나 if 문은 false와 동일한 0을 고려하기 때문에 검사를 피하고 직접 작성할 수 있습니다.

    if (substr_count($a, 'are')) {
    

    존재하지 않는지 확인하려면! 운영자:

    if (!substr_count($a, 'are')) {
    
  24. ==============================

    24.

    어쩌면 당신은 이런 식으로 사용할 수 있습니다 :

    <?php
        findWord('Test all OK');
    
        function findWord($text) {
            if (strstr($text, 'ok')) {
                echo 'Found a word';
            }
            else
            {
                echo 'Did not find a word';
            }
        }
    ?>
    
  25. ==============================

    25.

    한 문자열이 다른 문자열에 포함되어 있는지 확인하려는 경우 preg_match ()를 사용하지 마십시오. 더 빨라질수록 strpos () 또는 strstr ()을 대신 사용하십시오. (http://in2.php.net/preg_match)

    if (strpos($text, 'string_name') !== false){
       echo 'get the string';
    }
    
  26. ==============================

    26.

    strpos가 인덱스 값으로 0을 반환 할 수 있기 때문에 동일한 연산자 / 동일하지 않은 연산자를 사용해야합니다. 삼항 연산자가 마음에 들면, 다음을 사용하는 것을 고려해보십시오 (조금 뒤에서 나는 인정할 것입니다) :

    echo FALSE === strpos($a,'are') ? 'false': 'true';
    
  27. ==============================

    27.

    문자열에 몇 가지 구체적인 단어가 포함되어 있는지 확인하려면 다음을 수행 할 수 있습니다.

    $badWords = array("dette", "capitale", "rembourser", "ivoire", "mandat");
    
    $string = "a string with the word ivoire";
    
    $matchFound = preg_match_all("/\b(" . implode($badWords,"|") . ")\b/i", $string, $matches);
    
    if ($matchFound) {
        echo "a bad word has been found";
    }
    else {
        echo "your string is okay";
    }
    

    예를 들어 이메일을 보낼 때 스팸을 피하는 데 유용합니다.

  28. ==============================

    28.

    strpos 함수는 잘 작동하지만 문단에서 단어를 대소 문자를 구분하지 않고 검사하려면 PHP의 stripos 함수를 사용할 수 있습니다.

    예를 들어,

    $result = stripos("I love PHP, I love PHP too!", "php");
    if ($result === false) {
        // Word does not exist
    }
    else {
        // Word exists
    }
    

    문자열에서 대 / 소문자를 구분하지 않는 부분 문자열이 처음 나타나는 위치를 찾습니다.

    문자열에 단어가 없으면 false를 반환하고 그렇지 않으면 단어의 위치를 ​​반환합니다.

  29. ==============================

    29.

    이것은 문자열이 단어로 해석되어야 함을 의미합니다 (아래 참고 참조).

    이 작업을 수행하고 구분 기호를 지정하는 한 가지 방법은 preg_split (doc)을 사용하는 것입니다.

    <?php
    
    function contains_word($str, $word) {
      // split string into words
      // separators are substrings of at least one non-word character
      $arr = preg_split('/\W+/', $str, NULL, PREG_SPLIT_NO_EMPTY);
    
      // now the words can be examined each
      foreach ($arr as $value) {
        if ($value === $word) {
          return true;
        }
      }
      return false;
    }
    
    function test($str, $word) {
      if (contains_word($str, $word)) {
        echo "string '" . $str . "' contains word '" . $word . "'\n";
      } else {
        echo "string '" . $str . "' does not contain word '" . $word . "'\n" ;
      }
    }
    
    $a = 'How are you?';
    
    test($a, 'are');
    test($a, 'ar');
    test($a, 'hare');
    
    ?>
    

    달려라.

    $ php -f test.php                   
    string 'How are you?' contains word 'are' 
    string 'How are you?' does not contain word 'ar'
    string 'How are you?' does not contain word 'hare'
    

    참고 : 여기서 우리는 기호의 모든 연속에 대한 단어를 의미하지는 않습니다.

    실용적인 단어 정의는 PCRE 정규 표현 엔진입니다. 여기서 단어는 단어 문자로만 구성된 부분 문자열이며 단어가 아닌 문자로 구분됩니다.

  30. ==============================

    30.

    문자열은 아래 함수로 검사 할 수 있습니다.

    function either_String_existor_not($str, $character) {
        if (strpos($str, $character) !== false) {
            return true;
        }
        return false;
    }
    
  31. from https://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word by cc-by-sa and MIT lisence