Jonas Ivy V. Imperial
Jonas Ivy V. Imperial

Reputation: 125

php: How to find a string in a paragraph

Let's say that I have:

$strPar = "This is a simple paragraph that we will use for the questioning";
$strFindMe = "that";

How will I check if $strPar contains $strFindMe?

Upvotes: 1

Views: 5772

Answers (6)

user2339411
user2339411

Reputation:

$strPar = "This is a simple paragraph that we will use for the questioning";
$strFindMe = "THAT";//Find the position of the first occurrence of a case-insensitive substring in a string
$exists = strpos($strPar, $strFindMe);
  if ($exists !== false) {
    // substring is in the main string
  }

Upvotes: 0

user2339411
user2339411

Reputation:

$string = "This is a strpos() test";
$pos = strpos($string, "i", 3);

    if ($pos === false) {
     print "Not found\n";
}else{
     print "Found at $pos!\n";
}

Upvotes: 1

6339
6339

Reputation: 475

Check with strpos() function, it is case sensitive!

if( strpos($strPar, $strFindMe) ) {  //it return a boolean value
   echo "String Found";
}

Upvotes: 0

Bart Friederichs
Bart Friederichs

Reputation: 33531

Fastest way is to use strpos:

  $exists = strpos($strPar, $strFindMe);
  if ($exists !== false) {
    // substring is in the main string
  }

Upvotes: 2

jtomaszk
jtomaszk

Reputation: 11201

<?php
$strPar = "This is a simple paragraph that we will use for the questioning";
$strFindMe   = "that";
$pos = strpos($strPar, $strFindMe);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

Upvotes: 0

Orangepill
Orangepill

Reputation: 24655

try something like this

if (false !== strpos($strPar, $strFindMe ) )

Upvotes: 1

Related Questions