-3

This is example:

 $haystack = "451516178jjgsjkjjssbbziznbdkbnjv.bvkljk_isikjsksjkthisjkhkjhkjh4364765467";
 $needle = "this";

I need true or false. How to do that?

I think with preg_match but I dont know how.

4 Answers4

7

You don't need regex to do this. As already mentioned in comments use strpos():

if(strpos($heystack, $needle) !== false) {
// contains
}
The Mask
  • 17,007
  • 37
  • 111
  • 185
1

Try this:

$haystack = "451516178jjgsjkjjssbbziznbdkbnjv.bvkljk_isikjsksjkthisjkhkjhkjh4364765467";
$needle = "this";

$string = strpos($haystack, $needle);

if($string === false)
 echo "false";
else
 echo "true";
user3690206
  • 37
  • 1
  • 1
  • 8
1

You can try strpos functionality.

Example:

$title = "this is a string123";
if ((strpos($title,'is')=== false) {
    echo 'false';
else
   echo 'true';
}

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

Govind
  • 2,482
  • 1
  • 28
  • 40
  • 1
    you cannot use (!) in this case because this will fail if the occurence is in position `0` (which falls into !). you must use `=== false` – user1978142 Jun 17 '14 at 06:43
  • actually no problem, you better change this because some users like to downvote without an explanation. and no i wont downvote just want to clear this up – user1978142 Jun 17 '14 at 06:45
1

This is how you do it with regex:

$needle = "/this/";
echo preg_match($needle, $haystack);


This will return 1 one if it matches else 0

jayantS
  • 817
  • 13
  • 29