PHP Function: String Lesson

String is a type in PHP and it is a series of characters. There are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode.
However I have a good news as well as there is no limitation in string length in PHP (Of course available memory and PHP ini settings can reduce it).

You can set a string in three different ways. It's up to you what method do you use, all have pros.

  1. The first and most known way is to specify a string in single quotes. Similar to other programing languages you need to use backslash '\' if you want to display a single quote in your text.

    $str_1 = 'Demo text';
    $str_2 = 'Demo text with single quote(\')';
    echo $str_1;
    echo $str_2;     
  2.  The second option to use double quotes. Between double quotes you can use more escape character in your string. Besides this if you put a variable in the string then PHP will interpret it and display the content of the variable.

        $str_2 = "Demo text with double quote(\")\r\n";
        $demo = "Internal";
        $str_3 = "This is a $demo string";
        echo $str_1;
        echo $str_2;
        echo $str_3;

  3. And last you can use heredoc as well. In this case you define your text between heredoc identifiers. In this case it is DEMO. You need to start it with the operator <<< and then the identifier. If you are ready you need to close the heredoc part by adding the identifier again at the beginning of a new line like this:

          $str_1 = <<<DEMO
          This is a heredoc message
          echo $str_1;

    As you can see the output is in the same formatting - text indent, new lines - as it was defined. You can use variables in case of heredoc as well as in case of double quoted strings.

The 2. case is the most interesting string definition version so let's see a bit more how to insert a variable inside a string. (You can use variables in case 3. as well but heredoc is not so common.)

Here's some useful PHP function for string

str_replace: Replace a part of a string with another string
str_ireplace: Case in-sensitive search and replace using array of strings
parameter: str_replace(“search” , “replace”, $string);

$search = array("good", "php", "html", "asp");
$final_string = str_replace($search, $replace, "All good sites uses PHP along with HTML or they use asp and html");
echo $final_string;
OUTPUT: All GOOD sites uses PHP along with HTML or they use ASP and HTML

strlen: Count the length of a string
parameter: strlen($string);

$string="This is my string";
echo "Length of the string = ".strlen($string);
OUTPUT: Length of the string = 17

trim: Removing empty space from both sides of a string (left & right)
ltrim: left trim rtrim: right trim
parameter: trim($string);
  $text= " This is my text";
  echo $text;
str_repeat: Repeating a string number of times
parameter: str_repeat (input string, int multiplier );

  echo str_repeat(“$”,10);
OUTPUT: $$$$$$$$$$

strrev: Reversing a string by using strrev function in PHP
paremeter: strrev($string);

  $st="This is a test";
  echo "The string = ".$st.";
OUTPUT: tset a si sihT

stristr: Searching for a presence of a string inside another string
parameter: strstr (“mainstring”, “searchstring”);

  $email = "";
  $domain = strstr ($email, "@");
  echo $domain;

split: Breaking a string to form array using delimiters
parameter: split(“string pattern” , “string”);

  $months="Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sep, Oct, Nov,Dec";
  echo "$mn[2]";

substr_count: Counting occurrence of sub string in main string
parameter: substr_count(“main string”,"search_string");

  $my_string="PHP is a server side script and learn PHP at";
  echo substr_count($my_string,"PHP");
OUTPUT: The above code will print 2

substr: Collecting part of a string
parameter: substr (string string, int start ,[ int length])

  $string1=substr($string,0,4); //can be positive negative
OUTPUT: String1: abcd String2: abcdefghijk String3: ghijkl

strtolower: function to change alphabets to Lower case
strtoupper: function to change characters to upper case letters
parameter: strtolower($string); strtoupper($string);

  $my_str="Hello Please Visit PLUS2NET.COM"; 


strcasecmp: Case insensitive string comparison
strcmp: Case sensitive string comparison
parameter: strcasecmp($string,$ string); strcmp($string,$ string);

  $str1="Hello World";
  $str2="hello world";
  echo strcasecmp($str1,$str2);
  echo strcmp ($str1,$str2);
OUTPUT: 0 -1 Separating domain and userid part from an email address using split

  echo $ar[0];
  echo $ar[1];
OUTPUT: mynam

strip_tags: Removing html tags within a string
parameter: strip_tags (input string ,[ string allowable tags])
ucwords: Converting first letter of every word to UpperCase
parameter: ucwords($string);

  $str="welcome to you can learn web programming here";
  echo $str;
OUTPUT: Welcome To You Can Learn Web Programming Here
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

1 comments: on "PHP Function: String Lesson"

Anonymous said...

Amiable fill someone in on and this post helped me alot in my college assignement. Say thank you you seeking your information.

:) ) ;) ) ;;) :D ;) :p :( ( :) :( :X =(( :-o :-/ :-* :| 8-} :) ] ~x( :-t b-( :-L x( =))
Post a Comment