- BASICS
- Quotes
- Constants
- Control Structures
- Reference
- Number Systems
- VARIABLES
- Definition
- Variable Variable
- Exists
- Type Casting
- OPERATORS
- Aritmetic
- Bitwise
- String
- Comparison
- Logical
- FUNCTION
- Definition
- Anonymous
- Reference
- Variable Arguments
- ARRAY
- Basics
- Operations
- Create
- Search
- Modify
- Sort
- Storage
- STRING
- String Basics
-
String Compare
- String Search
- String Replace
- String Format
- String Regexp
- String Parse
- Formating
- Json
- STREAMS
- File Open
- Read File
- Read Csv
- File Contents
- Context
- Ob_start
- OOP
- Object Instantiation
- Class Constructor
- Interfaces, Abstract
- Resource Visibility
- Class Constants
- Namespaces
- HTTP
- Headers
- File Uploads
- Cookies
- Sessions
Strict
Not using strict operator === can lead to variable type problems.
$string = "123abc";
echo $string == 123 ? 'true' : 'false'; // true (incorect)
// "123abc" -> (int) "123abc" -> 123
echo $string === 123 ? 'true' : 'false'; // false (corect)
Safe
Binary safe string comparison. Returns < 0 if str1 is less than str2 Returns > 0 if str1 is greater than str2 Returns 0 if they are equal
echo strcmp("a", "a"); // 0
echo strcasecmp("A", "a"); // 0 (case insensitive)
echo strcmp("A", "a"); // -1
echo strcmp("a", "A"); // 1
Binary safe string comparison (n characters)
$str = "hello John";
echo strncasecmp($str, "Hello World", 5); // 0
substr_compare()
// substr_compare($main_str, $str, $offset, $length, $flag_case)
echo substr_compare("abcde", "bc", 1, 2); // bc, bc = 0
echo substr_compare("abcde", "de", -2, 2); // de, de = 0
echo substr_compare("abcde", "cd", 1, 2); // bc, cd = -1
echo substr_compare("abcde", "bc", 1, 3); // bcd, bc = 1
// main_str offset is less than str offset
echo substr_compare("abcde", "BC", 1, 2, true); // bc, bc = 0
// Third parameter - case insenstivity
echo substr_compare("abcde", "abc", 5, 1); // Warning
// Offset is equal or greater than string legth
Similar
similar_text() returns the number of matching chars in both strings.
echo similar_text("cat", "can"); // 2
similar_text("cat", "can", $percent);
echo $percent; // 66,6
Levenshtein
Minimal number of characters you have to replace. Insert or delete to transform str1 into str2.
echo levenshtein('cat', 'can'); // 1
Soundex
Words pronounced similarly produce the same soundex key. Can be used to simplify searches in databases.
echo soundex('wise'); // W200
echo soundex('ways'); // W200
Metaphone
More precise than soundex. It is based in english pronunciation rules - limited global use.
echo soundex('programming') == soundex('programmer'); // TRUE
// P626 == P626
echo metaphone('programming') != metaphone('programmer'); // TRUE
// PRKRMNK != PRKRMR
Last update: 531 days ago