top of page
Search

Programming Exercises In PHP (Set I)

    <?php

    echo "Program to change case of input string ...\n";

    $title = readline("Enter Any String:");


    echo "In Upper Case : " . strtoupper($title)."\n";

    echo "In Lower Case : " . strtolower($title)."\n";

    echo "First Letter Capital :" . ucfirst($title)."\n";

    echo "Title Case :" . ucwords($title);

    ?>

      <?php

      echo "Program to check for a valid non-empty string.\n";

      $title = readline("Enter Any String:");

      if (isset($title) && trim($title) != "")

      echo "String is not empty!\n";

      else

      echo "String is empty !\n";


      echo "Using empty() Function ......\n";

      // Incorrect way to check for an empty string

      if (!empty($title))

      echo "String is not empty!\n";

      else

      echo "String is empty !\n";

      ?>

        <?php

        #Program to create substring using built function by removing whitespaces


        $original = readline("Enter Any String:");


        #removing white spaces from string using regular expression

        $str = preg_replace('/\s+/','',trim($original));


        echo "New String After Removing Whitespaces : " . $str."\n";


        $n = readline("How many characters to remove?");

        if ($n > 0) :

        $newStr = substr($str,$n);

        else :

        $newStr = substr($str,0,$n);

        endif;

        echo "New String Is : " . $newStr;


        ?>

          <?php

          #Program to reverse a string and repeat string


          $str = readline("Enter Any String:");

          $newStr = strrev($str);

          echo "String in Reverse : " . $newStr. "\n";


          $str = readline("Enter Any String To Repeat:");

          $n = (int)readline("How Many Times?:");

          $newStr = str_repeat($str,$n);

          echo "The Repeating String Is:".$newStr."\n";


          ?>

            function truncate_string($str,$maxLength=10,$holder="...")

            {

            if (strlen($str) > $maxLength) :

            return trim(substr($str,0,$maxLength)) . $holder;

            else :

            return $str;

            endif;

            }


            $str = readline("Enter Any String:");

            echo truncate_string($str);


            ?>

              <?php

              #Program to demonstrate use of ord() and chr() function in PHP

              $str = readline("Enter a Character:");

              echo "ASCII values of all characters....\n";

              $i=0;

              $length = strlen($str);

              while ($i < $length) :

              echo ord($str[$i]) . " ";

              $i++;

              endwhile;


              echo "\nASCII values of all characters....\n";

              for($i=0;$i<128;$i++)

              echo "[" . $i . " , " . chr($i) . " ] ";


              ?>

                <?php

                #Program to split string in chnuks of 3 chacracters

                $str = readline("Enter Any String :");

                $chunk = 3;


                $strArray = str_split($str, $chunk);


                print_r($strArray);


                ?>

                  <?php

                  #Program to compare two strings for their similar pronunciation

                  $str1 = "deep";

                  $str2 = "dip";

                  if (metaphone($str1) == metaphone($str2)) :

                  echo "Both Strings are Similar !";

                  else:

                  echo "Both Strings are Not Similar !";

                  endif;

                  ?>

                    <?php

                    #Program to parse a string with comma separated list

                    $networkDevices = "Gateways, Routers, Switches, Hubs, Repeaters, Servers, Hosts";

                    $devicesArray = explode(", ", $networkDevices);

                    foreach($devicesArray as $i):

                    print $i . "\n";

                    endforeach;

                    ?>

                      <?php

                      #Program to combine array elements in string

                      $devicesArray = array("Gateways", "Routers", "Switches", "Hubs", "Repeaters", "Servers", "Hosts");

                      $networkDevices = implode(", ", $devicesArray);


                      echo $networkDevices;

                      ?>

                        <?php

                        #Program to parse URL

                        $url = "http://www.mysite.com:80/group/post/php.html";

                        $parsedURL = parse_url($url);


                        foreach($parsedURL as $key=>$value) :

                        echo "$key : $value\n";

                        endforeach;

                        ?>

                          <?php

                          $text = readline("Enter Any Text:");

                          $words = preg_split('/[^0-9A-Za-z\']+/', $text, -1,PREG_SPLIT_NO_EMPTY| PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE);

                          print_r ($words);

                          echo count($words) . " words";

                          ?>

                          NOTE:

                          All above programs are designed to run on terminal. If you want to execute these programs from within browser, please remove readline() function and either set values within program to accept input or use HTML form.

                          Want to read more?

                          Subscribe to questionbankos.com to keep reading this exclusive post.

                           
                           
                           

                          Recent Posts

                          See All
                          Programming Using C++

                          Course Fee - 1200/- (Rupees Twelve Hundred Only) OR pay Rs. 10/- (Rupees Ten Only) as a session fee to join any session of your choice. Sessions are only for Members of this educational web site. Yo

                           
                           
                           
                          Sorting Technique - Counting Sort

                          Q.1) How Counting Sort works? Counting sort maintains a separate array to count occurrences of each element and stores them in another array as per their sorted position which is calculates on the bas

                           
                           
                           
                          bottom of page