-2

有没有办法喜欢做一个默认代码,我不知道如何解释,但我会尽我所能。

<?php
if ($test == "CODE-*AnyWordHere*"){
    echo "Yes";
}
?>

另一个例子:

<?php
if ($url == "http://stackoverflow.com/secretplace/index.html/?*AnyWordHere*"){
    echo "Yes";
}
?>
4

3 回答 3

1

I'm not exactly sure what you're looking for, but here are a few guesses.

If you want to check for a variable string in an if statement you can use either strstr or strpos to see if a string exists in another one. To use the example you provided, try the following:

<?php
if (strstr($test, "CODE-")) {
    echo "Yes";
}
?>

If you want to retrieve that variable part of the string you're checking, try this:

<?php
$str_to_check = "CODE-*AnyWordHere*"

if ($pos = strpos($test, $str_to_check) !== false)) {
    $code = substr($str_to_check, $pos, len($str_to_check));
    echo $code;
}
?>

If you simply want to using a variable string in a control statement, try this:

<?php
if ($test == "CODE-" . $any_word_here){
    echo "Yes";
}
?>

The period joins (concatenate) two strings together.

If none of these suffice, then I'm not sure what you are asking for.

于 2012-08-05T01:47:58.813 回答
0

如果我正确理解了您的问题,您正在寻找strstr它将匹配以查看字符串是否包含另一个这样的字符串:

<?php
    $email  = 'http://stackoverflow.com/secretplace/index.html/?someTerm';
    $domain = strstr($email, 'someTerm');
    if ($domain)
    {
        // Your 'someTerm' was found in the string.
    }
?>
于 2012-08-05T01:32:58.840 回答
0

您可以尝试使用strpos()或:

function strending($string, $keyword){
    return substr($string, strlen($string)-strlen($keyword), strlen($string)) == $keyword;
}

strending("hello world", "world"); //true
strending("hello world!", "world"); //false

不确定这是否是您的意思。您也可以尝试使用regexp.

于 2012-08-05T01:36:54.013 回答