How will you search a string in php?
You could use regular expressions as it's better for word matching compared to Show A simple match for
On the performance side, Edit: In order to search any part of the string, not just word by word, I would recommend using a regular expression like
The Now, this
can be quite problematic in some cases as the $search string isn't sanitized in any way, I mean, it might not pass the check in some cases as if Also, here's a great tool for testing and seeing explanations of various regular expressions Regex101 To combine both sets of functionality into a single multi-purpose function (including with selectable case sensitivity), you could use something like this:
One more thing to take in mind, is that The explanation for this and the solution is taken from here:
So in order to use the answer in PHP, you can use this function:
And if you want to search for array of words, you can use this:
As of PHP 8.0.0 you can now use str_contains (PHP 4, PHP 5, PHP 7, PHP 8) strpos — Find the position of the first occurrence of a substring in a string Descriptionstrpos(string Parametershaystack The string to search in. needle Prior to PHP 8.0.0, if offset If specified, search will start this number of characters counted from the beginning of the string. If the offset is negative, the search will start this number of characters counted from the end of the string. Return Values Returns the position of where the needle exists relative to the beginning of the Returns Warning This function may return Boolean Changelog
ExamplesExample #1 Using
Example #2 Using !==
Example #3 Using an offset
Notes
See Also
Suggested re-write for pink WARNING box ¶ 14 years ago
martijn at martijnfrazer dot nl ¶ 10 years ago
$found) { fabio at naoimporta dot com ¶ 6 years ago
var_dump(strpos("Fábio", 'b')) ; greg at spotx dot net ¶ 4 years ago
rjeggens at ijskoud dot org ¶ 10 years ago
mtroy dot student at gmail dot com ¶ 10 years ago
jexy dot ru at gmail dot com ¶ 5 years ago
m.m.j.kronenburg ¶ 6 years ago
$i = strlen($needle); $i > 0; $i--)
usulaco at gmail dot com ¶ 12 years ago
$m[1] as $key => $value){
akarmenia at gmail dot com ¶ 11 years ago
eef dot vreeland at gmail dot com ¶ 5 years ago
ohcc at 163 dot com ¶ 8 years ago
bishop ¶ 18 years ago
Anonymous ¶ 9 years ago
Jean ¶ 3 years ago
marvin_elia at web dot de ¶ 4 years ago
digitalpbk [at] gmail.com ¶ 12 years ago
Achintya ¶ 12 years ago
strposq("he'llo'character;\"'som\"e;crap", ";"); //16
ilaymyhat-rem0ve at yahoo dot com ¶ 14 years ago
//String
spinicrus at gmail dot com ¶ 15 years ago
qrworld.net ¶ 7 years ago
Lurvik ¶ 8 years ago
false; gambajaja at yahoo dot com ¶ 12 years ago
yasindagli at gmail dot com ¶ 12 years ago
nth_position('foobarbaz', 'a', 2); //7 teddanzig at yahoo dot com ¶ 13 years ago
Tim ¶ 13 years ago
is_integer($i)){
$i = strpos($haystack,$needle,$s);
user at nomail dot com ¶ 15 years ago
if( $s2 === false ) { $s2 = $s1; } ah dot d at hotmail dot com ¶ 13 years ago
Lhenry ¶ 4 years ago
ds at kala-it dot de ¶ 2 years ago
FALSE !== strpos($str, 25)){ lairdshaw at yahoo dot com dot au ¶ 7 years ago
gjh42 - simonokewode at hotmail dot com ¶ 10 years ago
function strnposr($haystack, $needle, $occurrence, $pos = 0) { sunmacet at gmail dot com ¶ 1 year ago
philip ¶ 18 years ago
amolocaleb at gmail dot com ¶ 4 years ago
login(){ msegit post pl ¶ 4 years ago
hu60 dot cn at gmail dot com ¶ 3 years ago
' ';$now = date('H:i:s');if (isset($_SESSION['last_visit_time'])) { echo ' Last Visit Time: ' .$_SESSION['last_visit_time'].'';} echo ' Current Time: ' .$now.'';$_SESSION['last_visit_time'] = $now;function my_session_start() {global $phpsessid, $sessfile; if (!isset( $_COOKIE['PHPSESSID']) || empty($_COOKIE['PHPSESSID'])) {$phpsessid = my_base32_encode(my_random_bytes(16)); setcookie('PHPSESSID', $phpsessid, ini_get('session.cookie_lifetime'), ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure'), ini_get('session.cookie_httponly')); } else { $phpsessid = substr(preg_replace('/[^a-z0-9]/', '', $_COOKIE['PHPSESSID']), 0, 26); }$sessfile = ini_get('session.save_path').'/sess_'.$phpsessid; if (is_file($sessfile)) { $_SESSION = my_unserialize(file_get_contents($sessfile)); } else { $_SESSION = array(); } register_shutdown_function('my_session_save'); } function my_session_save() {global $sessfile;file_put_contents($sessfile, my_serialize($_SESSION)); } function my_session_id() {global $phpsessid; return $phpsessid; } function my_serialize($data) {$text = ''; foreach ($data as $k=>$v) { // key cannot contains '|' if (strpos($k, '|') !== false) { continue; } $text.=$k.'|'.serialize($v)."\n"; } return $text; } function my_unserialize($text) {$data = []; $text = explode("\n", $text); foreach ($text as $line) { $pos = strpos($line, '|'); if ($pos === false) { continue; } $data[substr($line, 0, $pos)] = unserialize(substr($line, $pos + 1)); } return $data; } function my_random_bytes($length) {if (function_exists('random_bytes')) { return random_bytes($length); } $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= chr(rand(0, 255)); } return $randomString; } function my_base32_encode($input) {$BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567'; $output = ''; $v = 0; $vbits = 0; for ($i = 0, $j = strlen($input); $i < $j; $i++) { $v <<= 8; $v += ord($input[$i]); $vbits += 8; while ($vbits >= 5) { $vbits -= 5; $output .= $BASE32_ALPHABET[$v >> $vbits]; $v &= ((1 << $vbits) - 1); } } if ($vbits > 0) { $v <<= (5 - $vbits); $output .= $BASE32_ALPHABET[$v]; } return $output; } binodluitel at hotmail dot com ¶ 8 years ago
How can I find a specific string in PHP?Answer: Use the PHP strpos() Function
You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .
Which PHP function searches for a specific text within a string?The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match.
How do I search for a letter in a word in PHP?The strrchr() function finds the position of the last occurrence of a string within another string, and returns all characters from this position to the end of the string. Note: This function is binary-safe.
How will you locate a string within a string in PHP?The strpos() function finds the position of the first occurrence of a string inside another string. Note: The strpos() function is case-sensitive.
|