在字符后搜索字符串

时间:2013-02-20 03:33:47

标签: php

我知道如何在@之类的字符之后搜索字符串。它的代码是使用strcmp和strstr的组合。但是如果字符串不在

之后如何搜索字符串

由于

3 个答案:

答案 0 :(得分:0)

使用strpos。它为您提供了另一个字符串内部字符串的位置,否则为false。

http://php.net/manual/en/function.strpos.php

$position_of_substring = strpos($what_to_search_in, $what_to_search_for);
if( $position_of_substring === false )
{
    echo 'The string does not contain the string you are looking for.';
}
else
{
    echo 'Your substring is at position: ' . $position_of_substring;
}

答案 1 :(得分:0)

您应该使用名为'preg_match'的方法

此处指向doc http://php.net/manual/en/function.preg-match.php

的链接

这是一个例子:

$subject = "@as.asd.com";
$subject2 = "@asd.as.com";
$pattern = '/asd.com/';
echo preg_match ($pattern, $subject); // return 1 if does match
echo preg_match ($pattern, $subject2); // return 0 if doesn't match

答案 2 :(得分:0)

$pattern = '/asd.com/';
$subject has the string that you want to test

使用您要搜索的字符分解主题。例如/ @

  

$ parts = explode(“@”,$ subject);

检查@是否存在于字符串中。如果是,则比较和回显输出

if(count($parts) == 2)  
    echo preg_match ($pattern, $parts[1]);

请参阅官方文档中的preg_match