10 Useful PHP String-Related Functions

No need to elaborate much on this one. Here are ten very useful functions for manipulating strings. Some are very simple and straightforward, but are still very useful.

I would normally use these in class called something like Common_Strings, and make each function static but I’ll just provide them here as functions for you to use as you please.

1. Replace Last Occurrence of a String

/**
* Replace the last occurrence of a string.
* 
* @param string $search
* @param string $replace
* @param string $subject
* @return string
*/
function strReplaceLast ( $search, $replace, $subject ) {
			
	$lenOfSearch = strlen( $search );
	$posOfSearch = strrpos( $subject, $search );
			
	return substr_replace( $subject, $replace, $posOfSearch, $lenOfSearch );
			
}

2. Strip All Non-Alpha Numeric Characters and Spaces

/**
* Remove all characters except letters, numbers, and spaces.
* 
* @param string $string
* @return string
*/
function stripNonAlphaNumericSpaces( $string ) {
	return preg_replace( "/[^a-z0-9 ]/i", "", $string );
}

3. Strip All Non-Alpha Numeric Characters

/**
* Remove all characters except letters and numbers.
* 
* @param string $string
* @return string
*/
function stripNonAlphaNumeric( $string ) {
	return preg_replace( "/[^a-z0-9]/i", "", $string );
}

4. Remove All Non-Numeric Characters

/**
* Remove all characters except numbers.
* 
* @param string $string
* @return string
*/
function stripNonNumeric( $string ) {
	return preg_replace( "/[^0-9]/", "", $string );
}

5. Remove All Non-Alpha Characters

/**
* Remove all characters except letters.
* 
* @param string $string
* @return string
*/
function stripNonAlpha( $string ) {
	return preg_replace( "/[^a-z]/i", "", $string );
}

6. Remove All Excess White-Space

/**
* Transform two or more spaces into just one space.
* 
* @param string $string
* @return string
*/
function stripExcessWhitespace( $string ) {
	return preg_replace( '/  +/', ' ', $string );
}

7. Format a String for a URL Slug

/**
* Format a string so it can be used for a URL slug
* 
* @param string $string
* @return string
*/
function formatForUrl( $string ) {
			
	$string = stripNonAlphNumericSpaces( trim( strtolower( $string ) ) );
	return str_replace( " ", "-", stripExcessWhitespace( $string ) );
			
}

8. Format an URL Slug into a Human Readable String

/**
* Format a slug into human readable string
* 
* @param string $string
* @return string
*/
function formatFromUrl( $string ) {
	return str_replace( "-", " ", trim( strtolower( $string ) ) );
}

9. Get Unique Characters from a String

/**
* Get an array of unique characters used in a string. This should also work with multibyte characters.
* 
* @param string $string
* @return mixed
*/
function getUniqueChars( $string, $returnAsArray=true ) {
    $unique = array_unique( preg_split( '/(?<!^)(?!$)/u', $string ) );
    if ( empty( $returnAsArray ) ) {
		$unique = implode( "", $unique );
    }
    return $unique;
}

10. Generate a Random String

/**
* Generate a random string of specified length from a set of specified characters
* 
* @param integer $size Default size is 30 characters.
* @param string $chars The characters to use for randomization.
*/
function randomString( $size=30, $chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ) {

	$string = "";
	$length = strlen( $chars );

	for( $i=0; $i < $size; $i++ ) {
	    $string .= $chars{ rand( 0, $length ) };
	}

	return $string;

}
This entry was posted in PHP, Programming.

Leave a Reply