Append and Prepend a string

Description PHP

This function ties the $pre to the start of $string, if $pre is not already there. Also does the same to the end of the string with $post.

The Code Download

  1. <?php
  2. function TieString($string,$pre='',$post='') {
  3.     if (strlen($pre) > 0) {
  4.         //tie the pre - check if not already tied.
  5.         if (strpos($string,$pre) !== 0) {
  6.             $string = $pre . $string;
  7.         }
  8.     }
  9.     if (strlen($post) > 0) {
  10.         //tie the post - check if not already tied
  11.         //get sub
  12.         $t = substr($string,(strlen($string) - strlen($post)),strlen($post));
  13.         if ($t != $post) {
  14.             $string .= $post;
  15.         }
  16.     }
  17.     return $string;
  18. }
  19. ?>

Credits

Added by JC on 12th November 2007

Comments

There are no comments about this snippet.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment