Strings in PHP PHP

For the most part, the string functions create strings from other strings or report about the properties of a string. The exception is the eval function, which executes a string as if it were a line of code in your PHP script.

array count_chars(string data, integer mode) stringcount_chars(string data, integer mode)
The count_chars function analyzes a string by the characters present. The modeargument controls the return value. Modes 0, 1 and 2 return an array. Modes 3 and 4 return a string. If mode is left out, mode 0 is used. If mode is 0, an array is returned indexed by ASCII codes, 0–255. Each element is setwith the count for that character. If mode is 1, only the elements with count greater thanzero are returned. If mode is 2, only the elements with count equal to zero are returned. Mode 3 returns a string containing each character appearing in the input string. Mode 4 contains a string containing all characters not appearing in the input string.

<?
//print counts for characters found
foreach(count_chars("Core PHP", 1) as
$key=>$value)
{
print("$key:
$value<BR>\n");
}
//print list of characters found
print("Characters: '" . count_chars("Core
PHP", 3) . "'<BR>\n");
?>

eval(string phpcode)
The eval function attempts to execute the phpcode argument as if it were a line in your PHP script. As with all strings, double quotes will cause the string to be evaluated for embedded strings and other special characters, so you may wish to use single quotes or escape dollar signs with backslashes.

In some ways, eval is like include or require. Beyond the obvious difference that eval works on strings instead of files, eval starts in a mode where it expects PHP code. If you need to switch to a mode where plain HTML is passed directly to the browser, you will need to insert a closing PHP tag (?>). Why would you ever want to execute eval on a string that contained plain HTML? Probably because the code was stored in a database.

Be extremely careful when calling eval on any string that contains data that at any time came from form variables. This includes database fields that were originally set through a form. When possible, use nested $ operators instead of eval.

<?
//Contrived example
//eval() line could be replaced with
$$varName = 1;
$varName = "myValue";
eval("\$$varName = 1;");
print($myValue . "<BR>\n");
//More realistic simulation of using eval
//on data from a database
$code_from_database = "<B><?
print(date(\"Y-m-d\")); ?></B>";
eval("?>" . $code_from_database);
?>

string sprintf(string format, ...)
The sprintf function operates identically to the printf function, except that instead of sending the assembled string to the browser, the string is returned. See the description of printf for a detailed discussion. This function offers an easy way to control the representation of numbers. Ordinarily PHP may print a double with no fraction

<?
$x = 3.00;
//print $x as PHP default
print($x . "<BR>\n");
//format value of $x so that
//it show two decimals after
//the decimal point
$s = sprintf("%.2f", $x);
print($s . "<BR>\n");
?>

string str_repeat(string text, integer count)
The str_repeat function returns a string consisting of the text argument repeated the number of times specified by the count argument.

<?
print(str_repeat("PHP!<BR>\n", 10));
?>

integer strcasecmp(string first, string second)
The strcasecmp function operates identically to strcmp with the exception that upper- and lowercase letters are treated as being identical. Check out soundex, metaphone, and similar_text for alternative ways of comparing strings.

<?
$first = "abc";
$second = "aBc";
if(strcasecmp($first, $second) == 0)
{
print("strings are equal");
}
else
{
print("strings are not equal");
}
?>

strchr
This function is an alias to strstr.

integer strcmp(string first, string second)
The strcmp function compares the first string to the second string. A number less than
zero is returned if the first string is less than the second. Zero is returned if they are equal.
A number greater than zero is returned if the first string is greater than the second string. Comparisions are made by ASCII values. This function is safe for comparing binary data. Check out soundex, metaphone, and similar_text for alternative ways of comparing strings.

<?
$first = "abc";
$second = "xyz";
if(strcmp($first, $second) == 0)
{
print("strings are equal");
}
else
{
print("strings are not equal");
}
?>

integer strcspn(string text, string set)
The strcspn function returns the position of the first character in the text argument that is part of the set argument. Compare this function to strspn.

<?
$text = "red cabbage";
$set = "abc";
$position = strcspn($text, $set);
// prints 'red '
print(substr($text, 0, $position));
?>

string stristr(string text, string substring)
The stristr function is a case-insensitive version of strstr, below. A portion of the text argument is returned starting from the first occurrence of the substring argument to the end.

<?
$text = "Although he had help, Leon is the author
of
this book.";
print("Full text: $text BR>\n");
print("Looking for 'leon':" . stristr($text,
"leon"));
?>

integer strlen(string text)
Use the strlen function to get the length of a string.

<?
$text = "a short string";
print("'$text' is " . strlen($text) . "
characters long.");
?>

integer strpos(string data, string substring, integer offset)
The strpos function returns the position of the substring argument in the data argument. If the substring argument is not a string, it will be treated as an ASCII code. If the substring appears more than once, the position of the first occurrence is returned. If the substring doesn't exist at all, then FALSE is returned. The optional offset argument instructs PHP to begin searching after the specified position. Positions are counted starting with zero.

This function is a good alternative to ereg when you are searching for a simple string. It carries none of the overhead involved in parsing regular expressions. It is safe for use with binary strings.

<?
$text = "Hello, World!";
//check for a space
if(strpos($text, 32))
{
print("There is a
space in '$text'<BR>\n");
}
//find where in the string World appears
print("World is at position " .
strpos($text, "World") . "<BR>\n");
?>

strrchr
This is an alias for strrpos.

integer strrpos(string text, string character)
The strrpos function operates similarly to strpos. It returns the last occurrence of the second argument in the first. However, only the first character of the second argument is used. This function offers a very neat way of chopping off the last part of a path, as in the example below.

<?
//set test string
$path = "/usr/local/apache";
//find last slash
$pos = strrpos($path, "/");
//print everything after the last slash
print(substr($path, $pos+1));
?>

integer strspn(string text, string set)
The strspn function returns the position in the first character in the text argument that is not part of the set of characters in the set argument. Compare this function to strcspan.

<?
$text = "cabbage";
$set = "abc";
$position = strspn($text, $set);
// prints 'cabba'
print(substr($text, 0, $position));
?>

string strstr(string text, string substring)
The strstr function returns the portion of the text argument from the first occurrence of the substring argument to the end of the string. If substring is not a string, it is assumed to be an ASCII code.

An empty string is returned when substring is not found in text. You can use it as a faster alternative to ereg if you test for an empty string, as in the example below. The stristr function is a case-insensitive version of this function.

<?
$text = "Although this is a string, it's not very
long.";
if(strstr($text, "it") != "")
{
print("The string contains 'it'.BR>/n");
}
?>

string strtok(string line, string separator)
The strtok function pulls tokens from a string. The line argument is split up into tokens separated by any of the characters in the separator string. The first call to strtok must contain two arguments. Subsequent calls are made with just the separator argument, unless you wish to begin tokenizing another string.

<?
// create a demo string
$line = "leon\tatkinson\tleon@clearink.com";
// loop while there are still tokens
for($token = strtok($line, "\t");
$token != "";
$token = strtok("\t"))
{
print("token: $tokenBR>\n");
}
?>

string substr(string text, integer start, integer length)
Use the substr function to extract a substring from the text argument. A string is returned that starts with the character identified by the start argument, counting from zero. If start is negative, counting will begin at the last character of the text argument instead of the first and work backward.

The number of characters returned is determined by the length argument or the beginning and end of the string. If length is negative, the returned string will end that many characters from the end of the string. In any case, if the combination of start and length calls for a string of negative length, a single character is returned.

This function is safe for use with binary strings.

<?
$text = "My dog's name is Angus.";
//replace Angus with Gus
print(substr_replace($text, "Gus", 17, 5));
?>

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics