Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to binary safe comparison of two strings (string and substring) from an offset, up to length characters in PHP

4 Answers

0 votes
// int substr_compare(string $main_str , string $str , int $offset 
//                    [, int $length [, bool $case_insensitivity = false ]])

// offset - start position for the comparison. If negative - starts from end of string
// Returns < 0 if main_str < str, > 0 if main_str > str, and 0 if they are equal

echo substr_compare("abcdefg", "bcd", 1, 2) . "<br />" ;
echo substr_compare("abcdefg", "bcd", 1, 3) . "<br />" ; 
echo substr_compare("abcdefg", "bcd", 1, 4) . "<br />" ; 




/*
run:
    
0
0
1
    
*/

 



answered Jul 21, 2016 by avibootz
0 votes
// int substr_compare(string $main_str , string $str , int $offset 
//                    [, int $length [, bool $case_insensitivity = false ]])

// offset - start position for the comparison. If negative - starts from end of string
// Returns < 0 if main_str < str, > 0 if main_str > str, and 0 if they are equal

echo substr_compare("abcdefg", "bcd", -1, 2) . "<br />" ;
echo substr_compare("abcdefg", "bcd", -1, 3) . "<br />" ; 
echo substr_compare("abcdefg", "bcd", -1, 5) . "<br />" ; 
echo substr_compare("abcdefg", "bcd", -6, 3) . "<br />" ; 




/*
run:
    
1
1
1
0
    
*/

 



answered Jul 21, 2016 by avibootz
0 votes
// int substr_compare(string $main_str , string $str , int $offset 
//                    [, int $length [, bool $case_insensitivity = false ]])

// offset - start position for the comparison. If negative - starts from end of string
// Returns < 0 if main_str < str, > 0 if main_str > str, and 0 if they are equal

echo substr_compare("abcdefg", "bcd", 0, 3) . "<br />" ;
echo substr_compare("abcdefg", "bcd", 0, 4) . "<br />" ; 
echo substr_compare("abcdefg", "bcd", 0, 5) . "<br />" ; 




/*
run:
    
-1
-1
-1
    
*/

 



answered Jul 21, 2016 by avibootz
0 votes
// int substr_compare(string $main_str , string $str , int $offset 
//                    [, int $length [, bool $case_insensitivity = false ]])
 
// offset - start position for the comparison. If negative - starts from end of string
// Returns < 0 if main_str < str, > 0 if main_str > str, and 0 if they are equal
 
echo substr_compare("abcdefg", "bcd", 1, 3) . "<br />" ; 
echo substr_compare("abcdefg", "BCD", 1, 3) . "<br />" ;
echo substr_compare("abcdefg", "BCD", 1, 3, true) . "<br />" ; 
 
 
 
/*
run:
     
0
1
0
     
*/

 



answered Jul 21, 2016 by avibootz
...