0

I am trying to find if string contain ONLY substring 'Student Appt.' in it.

$string_could_be = "Student Appt.";
$string_could_be = "Student Appt.. Name Jonathan";

if ($string contains 'Student Appt.' only )
   Return True;
else
   Return False;  
4
  • 3
    Do you mean check if the strings are equal? Give some examples, and specify what you expect each to return (true or false). I'm just not sure what you mean by stressing the word "ONLY" Commented May 17, 2013 at 17:07
  • By ONLY do you mean equal? Then just use == Commented May 17, 2013 at 17:09
  • Thanks , I was using strpos and it was pulling even if string contains 'student Appt..Name jonathan) . Commented May 17, 2013 at 17:14
  • Sorry, I did not read question properly, adeneo's (see below) answer is correct i think. mine is wrong. Commented May 17, 2013 at 17:16

3 Answers 3

1
if (trim($string) === trim($str_string_1)) {
    // The strings are equal after removing spaces on the ends
}
Sign up to request clarification or add additional context in comments.

Comments

1

Equal string check:

return $string == 'Student Appt.';

Containing string check:

return strpos($string, 'Student Appt.');

Comments

0

You just need a simple === comparison:

$find = "Student Appt.";

$string = "Student Appt.";                  // true
$string = "Student Appt.. Name Jonathan";   // false

if($string === $find)
    return true;
else
    return false;

You could even remove the if/else block completely, and instead just use:

return $string === $find;

Comments

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.