+ 1
PHP Strings
What are strpos(), strstr(), strexplode(), other str tags in php?? How do they really work? please explain with examples
5 ответов
+ 1
in strpos(): first occurrence means -
*there is two cat in this string "i love cat, and cat loves me too!"
but strpos will only return position of first cat
*strpos return value of position of cat
that means cat is located after how many letters.
i=1
=2
l=3
o=4
v=5
e=6
=7
//output 7
*it will not return how many times "cat" is written.
+ 2
@kaushal
can you say what first occurence means??
and i cant find 7 cats in the 1st string 😅
also another output of strpos states " cat, and cat loves me too! "
can you explain this alone plz😅
+ 2
thanks @kaushal
+ 1
* strpos() : function finds the position of the first occurrence of a string inside another string.
echo strpos("i love cat, and cat loves me too!","cat");
//output : 7
Related functions:
strrpos() - Finds the position of the last occurrence of a string inside another string (case-sensitive)
stripos() - Finds the position of the first occurrence of a string inside another string (case-insensitive)
strripos() - Finds the position of the last occurrence of a string inside another string (case-insensitive)
* strstr() : function Find the first occurrence of a string and return the rest of the string:
echo strpos("i love cat, and cat loves me too!","cat");
//output : cat, and cat loves me too!
* explode() : function breaks a string into an array.
$str = "i love cat, and cat loves me too!";
print_r (explode(" ",$str));
//output : Array ( [0] => i [1] => love [2] => cat, [3] => and [4] => cat [5] => loves [6] => me [7] => too! )
*strlen() : function Returns the length of a string
echo strlen("i love cat, and cat loves me too!");
//output : 33
//some of the str functions
strnatcasecmp() Compares two strings using a "natural order" algorithm (case-insensitive)
strnatcmp() Compares two strings using a "natural order" algorithm (case-sensitive)
strncasecmp() String comparison of the first n characters (case-insensitive)
strncmp() String comparison of the first n characters (case-sensitive)
strpbrk() Searches a string for any of a set of characters
strrchr() Finds the last occurrence of a string inside another string
strrev() Reverses a string
strspn() Returns the number of characters found in a string that contains only characters from a specified charlist
strtok() Splits a string into smaller strings
strtolower() Converts a string to lowercase letters
strtoupper() Converts a string to uppercase letters
strtr() Translates certain characters in a string
substr() Returns a part of a string
+ 1
and in strstr() function return the rest of the string after function Find the first occurrence of a string
example :
echo strstr("i love cat, and cat loves me too!","cat");
//output cat, and cat loves me too!
echo strstr("how are you doing","are");
//output : are you doing
echo strstr("php is a good language","good");
//output : good language