payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


String Functions   in Category: PHP   by amit

🕙 Posted on 2023-05-08 at 21:39:12     Read in Hindi ...


More String Functions

lowercase and UPPERCASE functions

    There are different keywords used in different programming languages, for these pre-defined functions. In PHP, you will use strtolower() and strtoupper() functions to make every alphabets within a pair of single quotes or a pair of double quotes, in lowercase and uppercase respectively. ucfirst() and ucwords() functions are used to make the first alphabet in the string and every first character of all words (that is, also after blank space) in the string, Uppercase.

<?php
  $var_name = 'teA paRty, kitty paRty, poLitiCal paRty, etc.'; // mixture of lowercase and uppercase letters are used for illustration
  echo strtolower( $var_name ); // tea party, kitty party, political party, etc.
  echo '<br />';
  echo strtoupper( $var_name ); // TEA PARTY, KITTY PARTY, POLITICAL PARTY, ETC.
  echo '<br />';
  echo ucfirst( $var_name ); // TeA paRty, kitty paRty, poLitiCal paRty, etc.
  echo '<br />';
  echo ucwords( $var_name ); // TeA PaRty, Kitty PaRty, PoLitiCal PaRty, Etc.
?>

Trimming and Checking within a String

    You have seen uses of blank spaces in various examples of previous page. Some of these blank spaces can be trimmed with trim() function. You can use strchr() function (to check a character) and strstr() function (to check many characters). In both strchr() and strstr() functions, two arguments are placed: the first argument is the name of variable or constant, and the second argument is a character or many character within quotes respectively.

<?php
  $var_name = '   P A R T Y   '; // three blank spaces are added before and after the word 'P A R T Y'
  echo trim( $var_name ); // only blank spaces before and after first and last character will be removed.
  $another_var = 'teA paRty, kitty paRty, poLitiCal paRty, etc.'; // another variable is used
  echo '<br />';
  echo strchr( $another_var, 'k' ); // case-sensitive
  echo '<br />';
  echo strstr( $another_var, 'kitty' ); // case-sensitive
?>

P A R T Y
kitty paRty, poLitiCal paRty, etc.
kitty paRty, poLitiCal paRty, etc.

    Surprisingly, you will get above output which not only find the character or the word, but also displays all characters after it. Therefore, you should use these two functions, strchr() and strstr() wisely. stristr() function is similar to strstr() function, but the former deals with case-insenstive character check.

Replacing, Repeating and Finding Position within a String

    Order of argument is important in every pre-defined function, and also in user-defined functions. That is, you should carefully read the official documentation of the concerned programming languages. str_replace() function requires three arguments: first is source character(s), second is target character(s) and the third argument is the STRING or variable name in which you will find and replace the first argument by second argument.

<?php
  $other_var = 'teA paRty, kitty paRty, poLitiCal paRty, etc.'; // other variable name is used
  echo str_replace( 'kitty', 'litti', $other_var ); // 'litti' is a famous dish in Bihar (India)
  echo '<br />';
  echo (int) str_replace( '−', '-', '−1' ); // when special character − is used by some website, you can replace them with - (dash) symbol
?>

teA paRty, litti paRty, poLitiCal paRty, etc.
-1

    str_repeat() function takes two argument, first one is STRING value or variable name, and the second argument is number of times (integer value) to repeat that string. strpos() function also takes two arguments: first is the STRING value or variable name, and the second is the character(s) whose position is to be found. Only first character's position will be returned. stripos() function is same as strpos() function, but the former finds the position of case-insenstive character, whereas the latter finds the position of case-sensitive character.

    substr() function takes three arguments to create a sub-string: first argument is the parent string or variable name, second argument is the starting position, and third argument is the number of characters to be extracted (ending position will be one less position than third argument as index number).

<?php
  $my_var = 'teA paRty, kitty paRty, poLitiCal paRty, etc.';
  echo str_repeat( $my_var, 3 ); // the string will repeated three times in this case
  echo '<br />';
  echo strpos( $my_var, 'kitty' ); // case-sensitive
  echo '<br />';
  echo substr( $my_var, 5, 10 ); // outputs aRty, kitt
?>

teA paRty, kitty paRty, poLitiCal paRty, etc.teA paRty, kitty paRty, poLitiCal paRty, etc.teA paRty, kitty paRty, poLitiCal paRty, etc.
11
aRty, kitt

Finding number of Words in a STRING

    You can find the number of words within a string value or a variable name, containing a string value using the str_word_count() function.

<?php
  $my_sentence = "Good Morning fri3ends, You are look4ing awe5some to6day."; // the string also contains numeric digits in addition of alphabets
  echo '<pre>'; // <pre> </pre> HTML element makes the output human-readable
  print_r( str_word_count( $my_sentence ) ); // outputs integer value of number of words
  echo '<br />';
  print_r( str_word_count( $my_sentence, 1 ) ); // outputs the result in an array, each word in an item
  print_r( str_word_count( $my_sentence, 2 ) ); // outputs the result in an associative array, starting position of each word is also displayed before it.
  print_r( str_word_count( $my_sentence, 1, "áàăÆæ345" ) ); // the third argument excludes all characters within a pair of double quotes or a pair of single quotes.
  echo '</pre>'; // ending PRE tag is necessary to display results properly
?>

12
Array
(
    [0] => Good
    [1] => Morning
    [2] => fri
    [3] => ends
    [4] => You
    [5] => are
    [6] => look
    [7] => ing
    [8] => awe
    [9] => some
    [10] => to
    [11] => day
)
Array
(
    [0] => Good
    [5] => Morning
    [13] => fri
    [17] => ends
    [23] => You
    [27] => are
    [31] => look
    [36] => ing
    [40] => awe
    [44] => some
    [49] => to
    [52] => day
)
Array
(
    [0] => Good
    [1] => Morning
    [2] => fri3ends
    [3] => You
    [4] => are
    [5] => look4ing
    [6] => awe5some
    [7] => to
    [8] => day
   
)

    There are many more string functions, which are not frequently used, and are for specific purposes. These string functions will be explained later, whenever necessary.


Leave a Comment: