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 

(Continued from previous part...)

How To Reformat a Paragraph of Text?

You can wordwrap() reformat a paragraph of text by wrapping lines with a fixed length. Here is a PHP script on how to use wordwrap():

<?php
$string = "TRADING ON MARGIN POSES ADDITIONAL 
RISKS AND IS NOT SUITABLE FOR ALL 
        INVESTORS. 
A COMPLETE LIST OF THE RISKS ASSOCIATED WITH MARGIN TRADING IS
AVAILABLE IN THE MARGIN RISK DISCLOSURE DOCUMENT.";
$string = str_replace("\n", " ", $string);
$string = str_replace("\r", " ", $string);
print(wordwrap($string, 40)."\n");
?>

This script will print:

TRADING ON MARGIN POSES ADDITIONAL
RISKS AND IS NOT SUITABLE FOR ALL
    INVESTORS.   A COMPLETE LIST OF THE
RISKS ASSOCIATED WITH MARGIN TRADING IS
 AVAILABLE IN THE MARGIN RISK DISCLOSURE
DOCUMENT.

The result is not really good because of the extra space characters. You need to learn preg_replace() to replace them with a single space character.

How To Convert Strings to Upper or Lower Cases?

Converting strings to upper or lower cases are easy. Just use strtoupper() or strtolower() functions. Here is a PHP script on how to use them:

<?php
$string = "PHP string functions are easy to use.";
$lower = strtolower($string);
$upper = strtoupper($string);
print("$lower\n");
print("$upper\n");
print("\n");
?>

This script will print:

php string functions are easy to use.
PHP STRING FUNCTIONS ARE EASY TO USE.

How To Convert the First Character to Upper Case?

If you are processing an article, you may want to capitalize the first character of a sentence by using the ucfirst() function. You may also want to capitalize the first character of every words for the article title by using the ucwords() function. Here is a PHP script on how to use ucfirst() and ucwords():

<?php
$string = "php string functions are easy to use.";
$sentence = ucfirst($string);
$title = ucwords($string);
print("$sentence\n");
print("$title\n");
print("\n");
?>

This script will print:

Php string functions are easy to use.
Php String Functions Are Easy To Use.

How To Compare Two Strings with strcmp()?

PHP supports 3 string comparison operators, <, ==, and >, that generates Boolean values. But if you want to get an integer result by comparing two strings, you can the strcmp() function, which compares two strings based on ASCII values of their characters. Here is a PHP script on how to use strcmp():

<?php
$a = "PHP is a scripting language.";
$b = "PHP is a general-purpose language.";
print('strcmp($a, $b): '.strcmp($a, $b)."\n");
print('strcmp($b, $a): '.strcmp($b, $a)."\n");
print('strcmp($a, $a): '.strcmp($a, $a)."\n");
?>

This script will print:

strcmp($a, $b): 1
strcmp($b, $a): -1
strcmp($a, $a): 0

As you can see, strcmp() returns 3 possible values:

  • 1: The first string is greater than the section string.
  • -1: The first string is less than the section string.
  • 0: The first string is equal to the section string.

(Continued on next part...)

Part:   1  2  3   4  5 


Selected Developer Jobs:

More...