Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Script Tips - PHP Built-in Functions for Strings

By: FYICenter.com

Part:   1   2  3  4  5 

A collection of 19 tips on manipulating PHP strings. Clear answers are provided with tutorial exercises on string functions including strlen, trim, substr, chop, strpos, strcmp, split, etc. Topics included in this collection are:

  1. How To Get the Number of Characters in a String?
  2. How To Remove White Spaces from the Beginning and/or the End of a String?
  3. How To Remove the New Line Character from the End of a Text Line?
  4. How To Remove Leading and Trailing Spaces from User Input Values?
  5. How to Find a Substring from a Given String?
  6. What Is the Best Way to Test the strpos() Return Value?
  7. How To Take a Substring from a Given String?
  8. How To Replace a Substring in a Given String?
  9. How To Reformat a Paragraph of Text?
  10. How To Convert Strings to Upper or Lower Cases?
  11. How To Convert the First Character to Upper Case?
  12. How To Compare Two Strings with strcmp()?
  13. How To Convert Strings in Hex Format?
  14. How To Generate a Character from an ASCII Value?
  15. How To Convert a Character to an ASCII Value?
  16. How To Split a String into Pieces?
  17. How To Join Multiple Strings into a Single String?
  18. How To Apply UUEncode to a String?
  19. How To Replace a Group of Characters by Another Group?

How To Get the Number of Characters in a String?

You can use the "strlen()" function to get the number of characters in a string. Here is a PHP script example of strlen():

<?php 
print(strlen('It\'s Friday!'));
?>

This script will print:

12

How To Remove White Spaces from the Beginning and/or the End of a String?

There are 4 PHP functions you can use remove white space characters from the beginning and/or the end of a string:

  • trim() - Remove white space characters from the beginning and the end of a string.
  • ltrim() - Remove white space characters from the beginning of a string.
  • rtrim() - Remove white space characters from the end of a string.
  • chop() - Same as rtrim().

White space characters are defined as:

  • " " (ASCII 32 (0x20)), an ordinary space.
  • "\t" (ASCII 9 (0x09)), a tab.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).
  • "\r" (ASCII 13 (0x0D)), a carriage return.
  • "\0" (ASCII 0 (0x00)), the NULL-byte.
  • "\x0B" (ASCII 11 (0x0B)), a vertical tab.

Here is a PHP script example of trimming strings:

<?php
$text = "\t \t Hello world!\t \t ";
$leftTrimmed = ltrim($text);
$rightTrimmed = rtrim($text);
$bothTrimmed = trim($text);
print("leftTrimmed = ($leftTrimmed)\n");
print("rightTrimmed = ($rightTrimmed)\n");
print("bothTrimmed = ($bothTrimmed)\n");
?> 

This script will print:

leftTrimmed = (Hello world!              )
rightTrimmed = (                 Hello world!)
bothTrimmed = (Hello world!)

How To Remove the New Line Character from the End of a Text Line?

If you are using fgets() to read a line from a text file, you may want to use the chop() function to remove the new line character from the end of the line as shown in this PHP script:

<?php
$handle = fopen("/tmp/inputfile.txt", "r");
while ($line=fgets()) {
  $line = chop($line);
  # process $line here...
}
fclose($handle);
?> 

How To Remove Leading and Trailing Spaces from User Input Values?

If you are taking input values from users with a Web form, users may enter extra spaces at the beginning and/or the end of the input values. You should always use the trim() function to remove those extra spaces as shown in this PHP script:

<?php
$name = $_REQUEST("name");
$name = trim($name);
# $name is ready to be used...
?> 

(Continued on next part...)

Part:   1   2  3  4  5 


Selected Developer Jobs:

More...