douweicheng5532 2012-03-09 14:45
浏览 18
已采纳

搜索字符串中的字符

$str1 = "HEADINGLEY";
$str2 = "HDNGLY";

how can i seach string 1 to see if it contains all the characters from string 2 in the order they exist in string 2 and return a true or false value ?

Any help appreciated :D

if this helps, this is the code i have todate..

echo preg_match('/.*H.*D.*N.*G.*L.*Y.*/', $str1, $matches);

returns 0

  • 写回答

8条回答 默认 最新

  • dongmei5168 2012-03-09 14:50
    关注

    I guess I'd use preg_match

    http://us.php.net/manual/en/function.preg-match.php

    ...and turning $str2 into a regular expression using str_split to get an array and implode to turn it back into a string with ".*" as glue between characters.

    http://us.php.net/manual/en/function.str-split.php http://us.php.net/manual/en/function.implode.php

    Upate -- I should have suggested str_split instead of explode, and here is the code that will get you a regular expression from $str2

    $str2pat = "/.*" . implode(".*", str_split($str2)) . ".*/";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?