Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Match a string exactly

Tags:

string

php

match

$check = 'this is a string 111';
if ($check = 'this is a string') {
echo 'perfect match';
} else {
echo 'it did not match up';
}

But it returns perfect match everytime instead of it did not match up... I can not seem to get the string to match the case exactly it will only work if part of the string matches up.

If i try to complicate things a little using board code and regex patterns it becomes a nightmare.

if ($check = '/\[quote(.*?)\](.*?)\[\/quote\]/su') {
$spam['spam'] = true;
$spam['error'] .= 'Spam post quote.<br />';
}

So if the post only contained quote tags it would be considered spam and ditched but i can not seem to solve it perhaps my patterns are wrong.

like image 831
C0nw0nk Avatar asked Jan 20 '12 15:01

C0nw0nk


2 Answers

You need to use == not just =

$check = 'this is a string 111';
if ($check == 'this is a string') {
echo 'perfect match';
} else {
echo 'it did not match up';
}

= will assign the variable.

== will do a loose comparison

=== will do a strict comparison

See comparison operators for more information.

like image 152
Nick Avatar answered Sep 19 '22 23:09

Nick


For equality comparison you want the == operator. = is assignment.

if ($check = 'this is a string') {

should be

if ($check == 'this is a string') {

Don't worry, we've all done it. I still do :)

like image 33
Rob Agar Avatar answered Sep 18 '22 23:09

Rob Agar