2

I made this code

$message = 'This domain has strpos';
$links = array('domain.com', 'do.main');
$safe = strpos($message, $links);
return $message;

but got error errorHandler->error in my page, whats wrong with my code? did I do some missing code?

what I want just detect if the message have string in $links then do rest of my code

thanks for droping out

1

2 Answers 2

4

Since PHP has no elegant solution and all I see is full blown functions here's a simple one that will work

$arr will hold an array of string you do NOT want

// If string has illegal characters return false 
function str_allowed($str, $arr) {
    foreach ($arr as $bad_string) {
        if(strpos($str, $bad_string) !== false)
            return false; // bad string detected, return false
    }
    return true; // if we got this far means everything's good return true
}
Sign up to request clarification or add additional context in comments.

Comments

3

Please try this one will give expected output.

$message = 'This domain has strpos';
$links = array('domain.com', 'do.main');
$safe = array();
foreach($links as $key=>$result){
    if(strpos($message, $result) != false){
      $safe[$result] = "String is available";   
    } else{
      $safe[$result] = "String is not available";       
    }

}
print_r($safe);

1 Comment

I would use if(strpos($message, $result) !== false){ ... }, otherwise you are testing for whether the string appears at the start of the 'haystack' variable, not whether it is present at all. See php.net's comment on evaluating booleans with strpos.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.