Page 117 - Open Soource Technologies 304.indd
P. 117

Unit 5: Strings



            To learn how many times a smaller string occurs in a larger one, use substr_count( ):  Notes
            $number = substr_count(big_string, small_string);
                   Example:

            $sketch = <<< End_of_Sketch Well, there’s egg and bacon; egg sausage and bacon; egg and spam;
            egg bacon and spam; egg bacon sausage and spam; spam bacon sausage and spam; spam egg spam
            spam bacon and spam; spam sausage spam spam bacon spam tomato and spam; End_of_Sketch;
            $count = substr_count($sketch, “spam”); print(“The word spam occurs $count times.”); The word
            spam occurs 14 times.
            The substr_replace( ) function permits many kinds of string modifications:
            $string = substr_replace(original, new, start [, length ]);

            The function replaces the part of original indicated by the start (0 means the start of the string)
            and length values with the string new. If no fourth argument is given,substr_replace( ) removes
            the text from start to the end of the string.
            For instance:
            $greeting = “good morning citizen”; $farewell = substr_replace($greeting, “bye”, 5, 7); // $farewell
            is “goodbye citizen”
            Use a length value of 0 to insert without deleting:

            $farewell = substr_replace($farewell, “kind “, 9, 0); // $farewell is “goodbye kind citizen”
            Use a replacement of “” to delete without inserting:
            $farewell = substr_replace($farewell, “”, 8); // $farewell is “goodbye”
            Here’s how you can insert at the beginning of the string:

            $farewell = substr_replace($farewell, “now it is time to say “, 0, 0); // $farewell is “now it is time
            to say goodbye”’

            A negative value for start indicates the number of characters from the end of the string from
            which to start the replacement:

            $farewell = substr_replace($farewell, “riddance”, -3); // $farewell is “now it is time to say good
            riddance”
            A negative length indicates the number of characters from the end of the string at which to stop
            deleting:
            $farewell = substr_replace($farewell, “”, -8, -5); // $farewell is “now it is time to say good dance”

            5.7.2 Miscellaneous String Functions
            The strrev( ) function takes a string and returns a reversed copy of it:

            $string = strrev(string);
                   Example:

            echo strrev(“There is no cabal”); labac on si erehT

            The str_repeat( ) function takes a string and a count and returns a new string consisting of the
            argument string repeated count times:
            $repeated = str_repeat(string, count);





                                             LOVELY PROFESSIONAL UNIVERSITY                                   111
   112   113   114   115   116   117   118   119   120   121   122