<?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;
}
}
$string = 'How are you?';
$array = explode(" ", $string);
if (in_array('are', $array) ) {
echo 'Found the word';
}
20
如果要检查字符串是否包含多个特定单词,您可以执行以下操作:
$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";
}
例如,这有助于在发送电子邮件时避免垃圾邮件 .
11
虽然这些答案中的大多数都会告诉您字符串中是否出现子字符串,但是's usually not what you want if you'正在查找特定字词,而不是子字符串 .
<?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'
基本上,如果您是 having trouble finding words with characters specific to some languages ,例如德语,法语,葡萄牙语,西班牙语等(例如:ä,é,ô,ç,º,ñ),您可能希望在函数前面加上 mb_ . 因此,接受的答案将使用mb_strpos或mb_stripos(用于不区分大小写的匹配):
if (mb_strpos($a,'are') !== false) {
echo 'true';
}
$a = 'How are you?';
if (strpos($a, 'are') !== false) {
echo 'true';
}
请注意 !== false 的使用是故意的; strpos() 返回针字符串在haystack字符串中开始的偏移量,或者如果针不是't found. Since 0 is a valid offset and 0 is 111386 , we can',则返回布尔 false ,使用更简单的结构,如 !strpos($a, 'are') .
$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>';
$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>';
<?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.";
}
?>
56
可以使用以下函数检查字符串:
function either_String_existor_not($str, $character) {
if (strpos($str, $character) !== false) {
return true;
}
return false;
}
<?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";
}
?>
30 回答
如果您的搜索不区分大小写,则使用strstr()或stristr()将是另一种选择 .
使用stripos()使用 case-insensitve matching :
如果你想避免“假”和“真理”问题,你可以使用substr_count:
它比strpos慢一点,但它避免了比较问题 .
这是一个小实用函数,在这种情况下很有用
如果结果是
>0
,那么使用substr_count的许多答案都会检查 . 但由于if
语句将same as false视为零,因此可以避免直接检查和写入:要检查是否不存在,请添加
!
运算符:它可以通过三种不同的方式完成:
1- stristr()
2- strpos()
3- preg_match()
以下功能也有效,不依赖于任何其他功能;它仅使用本机PHP字符串操作 . 就个人而言,我不推荐这个,但你可以看到它是如何工作的:
测试:
为了找到一个“单词”,而不是一系列可能实际上是另一个单词的一部分的字母的出现,以下将是一个很好的解决方案 .
如果要检查字符串是否包含多个特定单词,您可以执行以下操作:
例如,这有助于在发送电子邮件时避免垃圾邮件 .
虽然这些答案中的大多数都会告诉您字符串中是否出现子字符串,但是's usually not what you want if you'正在查找特定字词,而不是子字符串 .
有什么不同?子字符串可以出现在其他词语中:
"are" "are"开头"are"
"are" "hare"结尾
"are"在"fares"中间
缓解这种情况的一种方法是使用与word boundaries(
\b
)结合的正则表达式:这种方法没有上面提到的相同的误报,但它确实有一些自己的边缘情况 . 字边界匹配非单词字符(
\W
),这些字符将不是a-z
,A-Z
,0-9
或_
. 这意味着数字和下划线将被计为单词字符,这样的场景将失败:"are" "What are you thinking?"
"are" in "lol u dunno wut those are4?"
如果你想要比这更准确的东西,你将不得不开始进行英语语法语法分析,这是一个非常大的蠕虫(并假设正确使用语法,无论如何,这并不总是给定) .
简易版
也许你可以使用这样的东西:
您可以使用正则表达式,与其他用户提到的strpos相比,它更适合单词匹配,对于诸如票价,关心,凝视等字符串,它也会返回true . 这可以通过使用单词边界在正则表达式中简单地避免 .
一个简单的匹配可能看起来像这样:
在性能方面,strpos的速度提高了大约三倍,并且考虑到,当我同时进行一百万次比较时,完成时需要preg_match 1.5秒,而strpos需要0.5秒 .
这意味着必须将字符串解析为单词(请参阅下面的注释) .
执行此操作和指定分隔符的一种方法是使用
preg_split
(doc):跑步给出了
Note: 这里我们不是指每个符号序列的单词 .
词的实际定义在某种意义上是PCRE正则表达式引擎,其中单词是仅由单词字符组成的子串,由非单词字符分隔 .
如果您只想检查另一个字符串中是否包含一个字符串,请不要使用
preg_match()
. 请改用strpos()
或strstr()
,因为它们会更快 . (http://in2.php.net/preg_match)我遇到了一些麻烦,最后我选择创建自己的解决方案 . 不使用regular expression引擎:
您可能会注意到,之前的解决方案不是将该词用作另一个词的前缀的答案 . 为了使用你的例子:
对于上面的示例,
$a
和$b
都包含$c
,但您可能希望您的函数告诉您只有$a
包含$c
.另一种选择是使用strstr()函数 . 就像是:
注意:strstr()函数区分大小写 . 对于不区分大小写的搜索,请使用stristr()功能 .
您需要使用相同/不相同的运算符,因为strpos可以返回0作为其索引值 . 如果您喜欢三元运算符,请考虑使用以下内容(似乎有点倒退我会承认):
我有点留下深刻印象,这里没有使用
strpos
,strstr
和类似功能的答案Multibyte String Functions(2015-05-08) .基本上,如果您是 having trouble finding words with characters specific to some languages ,例如德语,法语,葡萄牙语,西班牙语等(例如:ä,é,ô,ç,º,ñ),您可能希望在函数前面加上
mb_
. 因此,接受的答案将使用mb_strpos或mb_stripos(用于不区分大小写的匹配):如果您不能保证all your data is 100% in UTF-8,则可能需要使用
mb_
函数 .理解为什么The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)的一篇好文章是Joel Spolsky .
您可以使用
strstr
函数:Without using an inbuilt function:
您可以使用strpos()函数来查找另一个字符串中出现的一个字符串:
请注意
!== false
的使用是故意的;strpos()
返回针字符串在haystack字符串中开始的偏移量,或者如果针不是't found. Since 0 is a valid offset and 0 is 111386 , we can',则返回布尔false
,使用更简单的结构,如!strpos($a, 'are')
.要确定字符串是否包含另一个字符串,您可以使用PHP函数strpos() .
int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
CAUTION:
如果您正在搜索的针头位于干草堆的开头,它将返回位置0,如果您执行的
==
比较不起作用,则需要执行===
==
符号是比较并测试左侧的变量/表达式/常量是否与右侧的变量/表达式/常量具有相同的值 .===
符号是比较以查看两个变量/表达式/常量是否相等AND
具有相同的类型 - 即两者都是字符串或两者都是整数 .同意SamGoody和Lego Stormtroopr的评论 .
如果您正在寻找多个单词的 rank search results based on proximity/relevance 的PHP算法,那么只需使用PHP即可快速简便地生成搜索结果:
Issues with the other boolean search methods such as strpos(), preg_match(), strstr() or stristr()
无法搜索多个单词
结果没有任何结果
PHP method based on Vector Space Model and tf-idf (term frequency–inverse document frequency):
这听起来很难,但却非常容易 .
如果我们想在字符串中搜索多个单词,核心问题是我们如何为每个单词分配权重?
如果我们可以根据字符串整体的代表性来对字符串中的术语进行加权,我们可以通过与查询最匹配的结果来排序结果 .
这是矢量空间模型的想法, not far from how SQL full-text search works:
CASE 1
RESULT
CASE 2
RESULTS
CASE 3
RESULTS
有很多改进,但模型提供了一种从自然查询获得良好结果的方法,自然查询没有布尔运算符,如
strpos()
,preg_match()
,strstr()
或stristr()
.NOTA BENE
可选地在搜索单词之前消除冗余
从而减少了索引大小并减少了存储需求
磁盘I / O减少
更快的索引和更快的搜索速度 .
1. Normalisation
2. Stopword elimination
3. Dictionary substitution
将词语替换为具有相同或相似含义的其他词语 . (例如:用'饥饿'代替'饥饿'和'饥饿'的例子)
可以执行进一步的算法测量(滚雪球)以进一步将单词减少到其本质含义 .
用十六进制等值替换颜色名称
通过降低精度来减少数值是标准化文本的其他方法 .
RESOURCES
http://linuxgazette.net/164/sephton.html
http://snowball.tartarus.org/
MySQL Fulltext Search Score Explained
http://dev.mysql.com/doc/internals/en/full-text-search.html
http://en.wikipedia.org/wiki/Vector_space_model
http://en.wikipedia.org/wiki/Tf%E2%80%93idf
http://phpir.com/simple-search-the-vector-space-model/
strpos函数工作正常,但是如果你想检查段落中的单词,那么你可以使用
PHP
的stripos
函数 .例如,
查找字符串中第一次出现不区分大小写的子字符串的位置 .
如果字符串中不存在该单词,则它将返回false,否则它将返回该单词的位置 .
Look at strpos():
可以使用以下函数检查字符串:
在PHP中,验证字符串是否包含某个子字符串的最佳方法是使用这样的简单辅助函数:
说明:
strpos查找字符串中第一次出现区分大小写的子字符串的位置 .
stripos查找字符串中第一次出现不区分大小写的子字符串的位置 .
myFunction($haystack, $needle) === FALSE ? FALSE : TRUE
确保myFunction
始终返回布尔值并修复索引时的意外行为substring为0 .$caseSensitive ? A : B
选择strpos或stripos进行工作,具体取决于$caseSensitive
的值 .输出:
您应该使用不区分大小写的格式,因此如果输入的值在
small
或caps
中,则无关紧要 .这里stripos在heystack中找到针 without 考虑案例(小/帽) .
PHPCode Sample with output
使用strstr()和stristr()从字符串中查找单词出现的另一个选项如下所示: