PHP如果整个单词在字符串中

时间:2016-08-18 07:47:36

标签: php

我如何检查php中的字符串中是否存在整个单词?我在StackOverFlow上找到了一些回答:

if (preg_match($needle, $haystack))
另一种方式:

if (strpos($haystack, $needle) !== false)

这两种方法都在一定程度上起作用,但如果$haystack只包含$needle的一部分,这些方法也会返回true,这是一个问题。

例如,如果我尝试检查$haystack是否包含单词" PartialWord"但我让$needle等于"部分"

$needle = "Partial";
$haystack = "PartialWord-random)exampletextfoo-blabla";
if (preg_match($needle, $haystack))

它将返回true,即使它应该返回false,因为$needle等于" Partial" &安培;返回true $needle应该等于" PartialWord"。

有什么建议吗?

3 个答案:

答案 0 :(得分:4)

    public class LongestCommonSubsequence {

    private static HashMap<Container, Integer> cache = new HashMap<>();
    private static int count=0, total=0;
    public static void main(String sargs[]){
        Scanner scanner = new Scanner(System.in);
        String x=scanner.nextLine();
        String y=scanner.nextLine();
        int max=0;
        String longest="";
        for(int j=0;j<x.length();j++){
            String common=commonSubsequence(j,0, x, y);
            if(max<common.length()){
                max=common.length();
                longest=common;
            }
        }
        for(int j=0;j<y.length();j++){
            String common=commonSubsequence(j,0, y, x);
            if(max<common.length()){
                max=common.length();
                longest=common;
            }
        }
        System.out.println(longest);
        System.out.println("cache used "+count+" / "+total);
    }
    public static String commonSubsequence(final int startPositionX, int startPositionY, String x, String y){
        StringBuilder commonSubsequence= new StringBuilder();
        for(int i=startPositionX;i<x.length();i++){
            Integer index=find(x.charAt(i),startPositionY,y);
            if(index!=null){
                commonSubsequence.append(x.charAt(i));              
                if(index!=y.length()-1)
                    startPositionY=index+1;
                else 
                    break;
            }               
        }
        return commonSubsequence.toString();        
    }
    public static Integer find(char query, int startIndex, String target){
        Integer pos=cache.get(new Container(query, startIndex));
        total++;
        if(pos!=null){
            count++;
            return pos;
        }else{
            for(int i=startIndex;i<target.length();i++){
                if(target.charAt(i)==query){
                    cache.put(new Container(query, startIndex), i);
                    return i;
                }                   
            }
            return null;
        }           
        }
        }
    class Container{
            private Character toMatch;
            private Integer indexToStartMatch;

        public Container(char t, int i){
        toMatch=t;
        indexToStartMatch=i;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime
                * result
                + ((indexToStartMatch == null) ? 0 : indexToStartMatch
                        .hashCode());
        result = prime * result + ((toMatch == null) ? 0 : toMatch.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Container other = (Container) obj;
        if (indexToStartMatch == null) {
            if (other.indexToStartMatch != null)
                return false;
        } else if (!indexToStartMatch.equals(other.indexToStartMatch))
            return false;
        if (toMatch == null) {
            if (other.toMatch != null)
                return false;
        } else if (!toMatch.equals(other.toMatch))
            return false;
        return true;


        }       
    }

其中:

preg_match('/\b(express\w+)\b/', $needle, $haystack); // matches expression
preg_match('/\b(\w*form\w*)\b/', $needle, $haystack); // matches perform,
                                                     // formation, unformatted

请参阅escape sequences上有关PCRE的手册。

答案 1 :(得分:3)

最简单的解决方案是:

ReturnsForAnyArgs

$words= preg_split("/\W/", $haystack); return in_array($needle, $words); 是任何非单词字符。

答案 2 :(得分:2)

我认为最简单的解决方案是使用preg_match

If (preg_match("/\s" . $SearchWord . "\s/", $haystack)){
      Echo "found";
}else{
      Echo "not found";
}

它将寻找$SearchWord,每边都有一个空格,据我所知,这个词的定义。
所以在你的情况下它不会与Partialword的Partialword匹配,因为Partialword中没有空格。