How to Reduce the Number of Characters in a String on PHP

How to Reduce the Number of Characters in a String on PHP thumbnail
PHP scripts use string functions to handle text.

Using PHP scripts, you can model sequences of text characters as strings. The PHP language provides functions for manipulating strings, for example to return a section of a larger string as another string value. To reduce the number of characters in a string, you can use the substring function to create a copy of those characters you wish to retain within the string. You can opt to keep the original string in addition to the reduced one, or can simply replace the original string with the shortened version.

Instructions

    • 1

      Prepare your string within your PHP script. The following sample PHP code demonstrates creating a string variable with some arbitrary text stored in it:

      $my_text = "Here is the text content";

      Your own string can contain any characters you like, including letters, numbers and punctuation symbols. You can choose any variable name for your string, as long as it makes sense to you and is a valid name within the language.

    • 2

      Carry out the substring process on your string. The substring function takes parameters representing the original string, the position to start the operation at and the length of the desired string section. Decide what length you want your new string to be and which position you want it to start at within the existing string, remembering that the first character position in a string is zero. Use the following syntax structure to carry out the substring operation on your string variable:

      substr($my_text, 0, 10);

      This example copies the first 10 characters from the original string variable.

    • 3

      Store your new string section as a variable. If you wish to keep your original string variable, you can store the new section as a separate variable by amending your code as follows:

      $text_section = substr($my_text, 0, 10);

      The substring function returns the requested section of the string, so this line stores the returned string value as a variable. If you do not want to keep your original string, you can replace it by altering the code as follows:

      $my_text = substr($my_text, 0, 10);

      Once this line has executed, your string variable will only contain the characters you indicated as parameters to the substring function.

    • 4

      Use your new string variable within your script. You can refer to the new string on any line after the substring operation, as you would with any other variable. For example, the following line of code will write the new string variable to the browser as part of an HTML structure:

      echo "<p>".$text_section."</p>";

      Carry out whatever processing your website or application requires using your new string.

    • 5

      Save your PHP script, upload the file to your Web server and browse to the page to test it. If you simply want to check that the substring operation has worked, write the new string out using the echo command. If the code does not work as you expected it to, check your substring function parameters and make sure you are passing the correct values for the start position and length. Experiment with the code until it does what you need it to.

Tips & Warnings

  • Keeping a copy of your original string may be necessary depending on your project, but if it is not, your script will use fewer resources if you overwrite the original variable with the new one.

  • Failing to test your string functions in PHP with the kind of data they will encounter when your site goes live may cause unpredictable behavior.

Related Searches:

References

Resources

  • Photo Credit Photos.com/Photos.com/Getty Images

Comments

Related Ads

Featured