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 Join Multiple Strings into a Single String?

If you multiple strings stored in an array, you can join them together into a single string with a given delimiter by using the implode() function. Here is a PHP script on how to use implode():

<?php 
$date = array('01', '01', '2006');
$keys = array('php', 'string', 'function');
print("A formated date: ".implode("/",$date)."\n");
print("A keyword list: ".implode(", ",$keys)."\n");
?>

This script will print:

A formated date: 01/01/2006
A keyword list: php, string, function

How To Apply UUEncode to a String?

UUEncode (Unix-to-Unix Encoding) is a simple algorithm to convert a string of any characters into a string of printable characters. UUEncode is reversible. The reverse algorithm is called UUDecode. PHP offeres two functions for you to UUEncode or UUDecode a string: convert_uuencode() and convert_uudecode(), Here is a PHP script on how to use them:

<?php
$msgRaw = "
From\tTo\tSubject
Joe\tLee\tHello
Dan\tKia\tGreeting";
$msgEncoded = convert_uuencode($msgRaw);
$msgDecoded = convert_uudecode($msgEncoded);
if ($msgRaw === $msgDecoded) {
  print("Conversion OK\n");
  print("UUEncoded message:\n");
  print("-->$msgEncoded<--\n");
  print("UUDecoded message:\n");
  print("-->$msgDecoded<--\n");
} else {
  print("Conversion not OK:\n");
}
?>

This script will print:

Conversion OK
UUEncoded message:
-->M1G)O;0E4;PE3=6)J96-T#0I*;V4)3&5E"4AE;&QO#0I$86X)2VEA"4=R965T
#:6YG
`
<--
UUDecoded message:
-->
From    To      Subject
Joe     Lee     Hello
Dan     Kia     Greeting<--

The output shows you that the UUEncode string is a multiple-line string with a special end-of-string mark \x20.

How To Replace a Group of Characters by Another Group?

While processing a string, you may want to replace a group of special characters with some other characters. For example, if you don't want to show user's email addresses in the original format to stop email spammer collecting real email addresses, you can replace the "@" and "." with something else. PHP offers the strtr() function with two format to help you:

  • strtr(string, from, to) - Replacing each character in "from" with the corresponding character in "to".
  • strtr(string, map) - Replacing each substring in "map" with the corresponding substring in "map".

Here is a PHP script on how to use strtr():

<?php
$email = "joe@dev.fyicenter.moc";
$map = array("@" => " at ", "." => " dot ");
print("Original: $email\n");
print("Character replacement: ".strtr($email, "@.", "#_")."\n");
print("Substring replacement: ".strtr($email, $map)."\n");
?>

This script will print:

Original: joe@dev.fyicenter.moc
Character replacement: joe#dev_fyicenter_moc
Substring replacement: joe at dev dot fyicenter dot moc

To help you to remember the function name, strtr(), "tr" stands for "translation".

Part:   1  2  3  4  5  


Selected Developer Jobs:

More...