“String Match in PHP” Code-Antworten

Strpos in PHP


<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// 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";
}
?>

Kaotik

String Match in PHP

//str_contains ( string $haystack , string $needle ) : bool

if (str_contains('Foo Bar Baz', 'Foo')) {
  echo 'Found';
}
Faisal Sajib

String Match in PHP

You could use regular expressions as its better for word matching compared to
strpos, as mentioned by other users. A strpos check for are will also return 
true for strings such as: fare, care, stare, etc. These unintended matches can 
simply be avoided in regular expression by using word boundaries.

A simple match for are could look something like this:

$a = 'How are you?';

if (preg_match('/\bare\b/', $a)) {
    echo 'true';
}
Lokesh003

Ähnliche Antworten wie “String Match in PHP”

Fragen ähnlich wie “String Match in PHP”

Weitere verwandte Antworten zu “String Match in PHP” auf PHP

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen