How to make strpos case insensitive
You're looking for stripos()
If that isn't available to you, then just call strtolower()
on both strings first.
EDIT:
stripos()
won't work if you want to find a substring with diacritical sign.
For example:
stripos("Leży Jerzy na wieży i nie wierzy, że na wieży leży dużo JEŻY","jeży");
returns false
, but it should return int(68)
.
How to use PHP strpos not case sensitive?
Yeah, stripos()
is what you're looking for. Here's the manual page.
PHP strpos() IS case sensitive for me
stripos
is the function that you are looking for. strpos
is case sensitive
http://php.net/manual/en/function.stripos.php
How to perform case insensitive str_contains?
You want a case-insensitive version of str_contains()
The short answer is: There isn't one.
The long answer is: case-sensitivity is encoding and locale dependent. By the time you've added those are information to a hypothetical str_icontains()
, you've recreated mb_stripos()
. TL;DR - Don't do it.
PHP - String contains
If you need looser matching, use a case insensitive function or regex:
stristr($badge->getName() , $_POST['name'])
or
if( ! preg_match("/" . $_POST['name'] . "/i",$badge->getName()) ) {
In these suggestions stristr is the case insensitive version of strstr and /i is the case insensitive flag for regex
Contains case insensitive
Add .toUpperCase()
after referrer
. This method turns the string into an upper case string. Then, use .indexOf()
using RAL
instead of Ral
.
if (referrer.toUpperCase().indexOf("RAL") === -1) {
The same can also be achieved using a Regular Expression (especially useful when you want to test against dynamic patterns):
if (!/Ral/i.test(referrer)) {
// ^i = Ignore case flag for RegExp
strstr() function like, that ignores upper or lower case
From the manpage for strstr
:
STRSTR(3) Linux Programmer's Manual STRSTR(3)
NAME
strstr, strcasestr - locate a substring
SYNOPSIS
#include <string.h>
char *strstr(const char *haystack, const char *needle);
#define _GNU_SOURCE
#include <string.h>
char *<b><u>strcasestr</u></b>(const char *haystack, const char *needle);
DESCRIPTION
The strstr() function finds the first occurrence of the substring needle in
the string haystack. The terminating '\0' characters are not compared.
<b>The strcasestr() function is like strstr(3), but ignores the case of both
arguments.</b>
RETURN VALUE
These functions return a pointer to the beginning of the substring, or NULL if
the substring is not found.
So what you're looking for is strcasestr
.
Related Topics
Single and Double Quotes Together as HTML Attribute Value
Sending Multiple Attachment in an Email Using PHP
Remove Item from Array If It Exists in a 'Disallowed Words' Array
Pdo Fetch One Column from Table into 1-Dimensional Array
What Is the Use of '\G' Anchor in Regex
Unset a Column in a Multidimensional Array
Select Count() VS MySQL_Num_Rows();
Store Procedures in PHPmyadmin
Php, Simplexml, Decoding Entities in Cdata
How to "Validate" Human Names in Cakephp
Sqlstate[42S22]: Column Not Found: 1054 Unknown Column - Laravel
How to Implement Content-Disposition: Attachment
How to Find a Whole Word in a String in PHP Without Accidental Matches
How to Store and Retrieve Image Contents from the Database Using Laravel
How to Check Which PHP Extensions Have Been Enabled/Disabled in Ubuntu Linux 12.04 Lts
Warning: Imagejpeg() [Function:Imagejpeg]: Gd-Jpeg: Jpeg Library Reports Unrecoverable Error