PHP를 사용하여 단어가 다른 문자열에 포함되어 있는지 어떻게 확인할 수 있습니까?
의사 코드
text = "I go to school";
word = "to"
if ( word.exist(text) ) {
return true ;
else {
return false ;
}
텍스트에 단어가 있으면 true를 반환하는 PHP 함수를 찾고 있습니다.
필요에 따라 몇 가지 옵션이 있습니다. 이 간단한 예에서는 strpos()
아마도 사용하기에 가장 간단하고 직접적인 기능 일 것입니다. 당신이 결과에 뭔가를해야 할 경우 선호 할 수 있습니다 strstr()
또는 preg_match()
. 바늘로 문자열 대신 복잡한 패턴을 사용해야하는 경우 preg_match()
.
$needle = "to";
$haystack = "I go to school";
strpos () 및 stripos () 메서드 (stripos ()는 대소 문자를 구분하지 않음) :
if (strpos($haystack, $needle) !== false) echo "Found!";
strstr () 및 stristr () 메서드 (stristr은 대소 문자를 구분하지 않음) :
if (strstr($haystack, $needle)) echo "Found!";
preg_match 메서드 (정규식, 훨씬 더 유연하지만 느리게 실행 됨) :
if (preg_match("/to/", $haystack)) echo "Found!";
완전한 함수를 요청했기 때문에 다음과 같이 조합 할 수 있습니다 (needle 및 haystack의 기본값 포함).
function match_my_string($needle = 'to', $haystack = 'I go to school') {
if (strpos($haystack, $needle) !== false) return true;
else return false;
}
function hasWord($word, $txt) {
$patt = "/(?:^|[^a-zA-Z])" . preg_quote($word, '/') . "(?:$|[^a-zA-Z])/i";
return preg_match($patt, $txt);
}
$ word가 "to"이면 다음과 일치합니다.
- "듣기"
- "달로"
- "최신"
하지만:
- "함께"
- "우주 속으로"
사용하다:
return (strpos($text,$word) !== false); //case-sensitive
또는
return (stripos($text,$word) !== false); //case-insensitive
<?php
$text = "I go to school";
$word = "to"
$pos = strpos($text, $word);
if ($pos === false) {
return false;
} else {
return true;
}
?>
$text="I go to school";
return (strpos($text, 'to')!== false);
strpos의 올바른 사용법을 찾는 데 필요한 매뉴얼 페이지
또 다른 방법은 (이미 주어진 strpos 예제 외에도 'strstr'함수를 사용하는 것입니다.
if (strstr($haystack, $needle)) {
return true;
} else {
return false;
}
이러한 문자열 함수를 사용할 수 있습니다.
strstr — 첫 번째 문자열 찾기
stristr — 대소 문자를 구분하지 않는 strstr ()
strrchr — 문자열에서 마지막 문자 찾기
strpos — 문자열에서 하위 문자열이 처음 나타나는 위치를 찾습니다.
strpbrk — Search a string for any of a set of characters
If that doesn't help then you should use preg
regular expression
preg_match — Perform a regular expression match
@mrclay
cant' we simply do
"/(?:^|\w+)" . preg_quote($word, '/') . "(?:$|\w+)/i"
so that it either checks starting or whitespace, and ending or whitespace.
After searching so many times for a suitable php version, I decide to write my own contains function (with more than one parameter needles) and good to remember.
function contains($str,$contain)
{
if(stripos($contain,"|") !== false)
{
$s = preg_split('/[|]+/i',$contain);
$len = sizeof($s);
for($i=0;$i < $len;$i++)
{
if(stripos($str,$s[$i]) !== false)
{
return(true);
}
}
}
if(stripos($str,$contain) !== false)
{
return(true);
}
return(false);
}
Description of php contains:
contains($str,$arg)
$str: The string to be searched
$arg: The needle, more arguments divided by '|'
Examples:
$str = 'green house';
if(contains($str,"green"))
echo "we have a green house.";
else
echo "our house isn't green";
$str = 'green, blue, red houses';
if(contains($str,"green|red"))
echo "we have a green or red house.";
else
echo "we have a blue house.";
Use strpos
function in php .
$text = "I go to school";
$word = "to"
if (strpos($text,$word)) {
echo 'true';
}
'Program Tip' 카테고리의 다른 글
하위 프로세스 호출에서 종료 코드 및 stderr 가져 오기 (0) | 2020.12.03 |
---|---|
RDF 트리플은 무엇입니까? (0) | 2020.12.03 |
SQL에서 UPDATE는 항상 DELETE + INSERT보다 빠릅니까? (0) | 2020.12.03 |
여러 선택적 매개 변수를 C # 함수에 전달 (0) | 2020.12.03 |
WebView의 HTTP 요청 가로 채기 및 재정의 (0) | 2020.12.03 |