Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created June 27, 2018 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpfiddle/f6579f106f57c2f37f28383783680d12 to your computer and use it in GitHub Desktop.
Save phpfiddle/f6579f106f57c2f37f28383783680d12 to your computer and use it in GitHub Desktop.
[ Posted by Rakesh ] ddddddddddddddsddddddddddddddddddsddddddddddddddsddddddd
<?php
// Trimming String
$s1 = " Rakesh Kumar ";
//echo "Original String: ","<br/>";
//var_dump($s1);
//echo "<br/>Length of original string: ",strlen($s1); //Returns the length of a string strlen(string) Returns the length of a string on success, and 0 if the string is empty
//echo "<br/>CHOP:\t";
//var_dump(chop($s1)); //Remove whitespace and other predefined characters from the right end of a string. chop(string[,charlist]) Returns the modified string.
//echo "<br/>RTRIM:\t";
//var_dump(rtrim($s1)); //Remove whitespace and other predefined characters from the right end of a string. rtrim(string[,charlist]) Returns the modified string.
//echo "<br/>LTRIM:\t";
//var_dump(ltrim($s1)); //Remove whitespace and other predefined characters from the left end of a string. ltrim(string[,charlist]) Returns the modified string.
//echo "<br/>TRIM:\t";
//var_dump(trim($s1)); //Remove whitespace and other predefined characters from the both end of a string. trim(string[,charlist]) Returns the modified string
/*
Predefined Characters
"\0" - NULL
"\t" - tab
"\n" - new line
"\x0B" - vertical tab
"\r" - carriage return
" " - ordinary white space
*/
// Change Case function
$s2 = "THE QUICK BROWN";
//echo "<br/>LCFIRST:\t";
//var_dump(lcfirst($s2)); // Converts the first character of a string to lowercase. lcfirst(string) Returns the converted string
$s3 = "the quick brown";
//echo "<br/>UCFIRST:\t";
//var_dump(ucfirst($s3)); // Converts the first character of a string to uppercase. ucfirst(string) Returns the converted string
//echo "<br/>UCWORDS:\t";
//var_dump(ucwords($s3)); // Convert the first character of each word in the string to uppercase. ucwords(string) Returns the converted string
//echo "<br/>STRTOUPPER:\t";
//var_dump(strtoupper($s3)); // Convert all characters of the string to uppercase. strtoupper(string) Returns the converted string
//echo "<br/>STRTOLOWER:\t";
//var_dump(strtolower($s3)); // Convert all characters of the string to lowercase. strtolower(string) Returns the converted string
//echo "<br/>SUBSTR:\t<br/>";
//echo substr($s3,0,3), "<br/>"; // Returns a part of a string. substr(string[,start][,length])
/*index positions in string starts from 0 like array.*/
// Getting substring from a string.
$ss = "123456";
// substr(string,start[,length]) Returns a part of a string. If the start parameter is a negative number and length is less than or equal to start, length becomes 0.
// Trick - put cursor on start or end depending on start +- and move by one character counting 1, where you stop counting see cursor position characters after the cursor will be included.
// Conclusion - If start is positive, starting character is not included, if negiative it's included.
//echo substr($ss,1),"<br/>";
//echo substr($ss,4),"<br/>"; //56
//echo substr($ss,-4),"<br/>"; //3456
//echo substr($ss,3,2),"<br/>"; //45
//echo substr($ss,-4,-2),"<br/>"; //34
//echo substr($ss,-4,-3),"<br/>"; //3
//echo substr($ss,-7,-5),"<br/>"; //4..55
//echo substr($ss,3,2),"<br/>"; //45
//echo substr($ss,-3,2),"<br/>"; //45
$s4 = "aBcdefghi";
//echo strstr($s4, 97),"<br/>"; // The strstr() function searches for the first occurrence of a string inside another string and return the rest of the string depending on the before_search flag.
// strstr(string,search[,before_search]) search - number / string (if number match for ASCII value), before_search - false / true (Default - false) false - search term and next all characters, true- all previous characters except search term.
// This function is case-sensitive and binary safe.
//echo strstr($s4, "de"),"<br/>";
//echo strstr($s4, "de", true),"<br/>";
//echo strstr($s4, "c",true),"<br/>";
//echo stristr($s4, "C"),"<br/>"; // This function is case-sensitive and binary safe as strstr() except it's case-intensive.
//echo strstr($s4, "C"),"<br/>"; // When search is ASCII value strstr and stristr behaves exactly same.
//echo "stristr ",stristr($s4, "cdeM"),"<br/>";
//echo strstr($s4, "103"),"<br/>";
//echo "strrchr ",strrchr($s4, 'cdeM'),"<br/>"; // strrchr(string,char) == strrstr(string,search) Returns all characters from the last occurrence of a string within another string, to the end of the main string, or FALSE if the character is not found. Partical match can be possible with strrchr().
//echo strrchr("Hello world! What a beautiful day!","What");
//echo strrchr("helloabc",97);
// strchr() alis of strstr()
//echo "strchr ",strchr("Hello world! What a beautiful day!","Whatxx");
// Position of a string in another string
$s5 = "abcdabcdabcd";
//echo strpos($s5,"b"),"<br/>";
//echo strpos($s5,"B",2),"<br/>";
//echo strpos($s5,"b",2),"<br/>";
// strpos(string,find[,start]) Returns the position of the first occurrence of a string inside another string (case sensetive)
// stripos(string,find[,start]) Returns the position of the first occurrence of a string inside another string (case intensetive)
// strrpos(string,find[,start]) Returns the position of the last occurrence of a string inside another string (case sensetive)
// strripos(string,find[,start]) Returns the position of the last occurrence of a string inside another string (case intensetive)
// These 4 function are binary-safe.
// Array <--> String Functions
//explode(separator,string[,limit]) -Breaks a string into an array
// Possivle Values of limit
// Greater than 0 - Returns an array with a maximum of limit element(s)
// Less than 0 - Returns an array except for the last -limit elements()
// 0 - Returns an array with one element
//echo "<pre>";print_r(explode(",", "Rakesh,Devendra,Harish,Manav,Priyan"));
//echo "<pre>";print_r(explode(",", "Rakesh,Devendra,Harish,Manav,Priyan",2));
//echo "<pre>";print_r(explode(",", "Rakesh,Devendra,Harish,Manav,Priyan",0));
//echo "<pre>";print_r(explode(",", "Rakesh,Devendra,Harish,Manav,Priyan",-3));
//implode([separator,]array) or implode(array[,separator]) Joins all elements of array with seprator and returns a string.
$x = ["ram","sita","geeta"];
//echo implode(",",$x);
echo implode($x); //
exit;
echo "<br/>COUNT_CHARS:\t";
echo "<pre>";print_r(count_chars($s3,1)); //Returns information about characters used in a string count_chars(string[,mode]) Returns String or Array Depending on mode.
/*
Modes
0 - an array with the ASCII value as key and number of occurrences as value
1 - an array with the ASCII value as key and number of occurrences as value, only lists occurrences greater than zero
2 - an array with the ASCII value as key and number of occurrences as value, only lists occurrences equal to zero are listed
3 - a string with all the different characters used
4 - a string with all the unused characters
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment