search
Homephp教程php手册php中STRPOS查找失败返回的是FALSE注意事项

strpos() 函数返回字符串在另一个字符串中第一次出现的位置了,但是我们在按官方的手册来测试时发现一直返回false了,那么要如何来解决.

STRPOS用法:strpos(string,find,start)

我们看看strpos的一段代码:

//ajax获取分词 
public function get_segment_words() { 
    $title = I('title'); 
    $content = I('content'); 
    $Segment = M('Segment'); 
    $Segment_content = M('Segment_content'); 
    $segment_array = $Segment->getField('id,word'); 
    $setment_words = array(); 
    foreach ($segment_array as $id => $word) { 
        if (false !== strpos($content, $word) || false !== strpos($title, $word)) { 
            $segment_words[] = $word; 
        }   
    } 
    $segment_words = array_unique($segment_words); 
    $segment_words = implode(',', $segment_words); 
    echo $segment_words;exit; //ajax获取填充 
}

注意这句代码: 

if (false !== strpos($content, $word) || false !== strpos($title, $word)) { 
    //TODO 
}

如果匹配失败,strpos会返回bool类型的false,这个一定要注意,不然会出现什么情况呢?假如你要匹配的字符正好在第一个位置,对于strpos这个函数来说返回的是0,也就是位置0.这个时候实际上是匹配到了,结果返回0,如果这样判断就错了:

if (!strpos($content, $word))

举个例子:

内容:购物袋范德萨法拉点撒范德萨。

匹配:购物

如果要找内容里面是否有"购物"两个字,strpos会返回0,表示存在"购物"两个字,位置是0,因此要用false!=strpos($find,$content)来判断匹配是否成功.


本文地址:

转载随意,但请附上文章地址:-)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.