IT Community - Software Programming, Web Development and Technical Support

string functions and manipulations in php

This is a discussion on string functions and manipulations in php within the PHP Programming forums, part of the Web Development category; Hi, Strpos Vs Stripos Both function is used to findout the position of any charatcer in string content.The difference ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #61 (permalink)  
Old 11-09-2007, 09:07 PM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: string functions and manipulations in php

Hi,

Strpos Vs Stripos

Both function is used to findout the position of any charatcer in string content.The difference between these two function is Strpos is used to find position of first occurrence of a string and it is case sensitive while Stripos is case insensitive

Note: stripos support only PHP5 and higher.

PHP Code:
<?php
echo strpos("helLo","l");
?>
Output
2

PHP Code:
<?php
echo strpos("helLo","L");
?>
Output
3

PHP Code:
<?php
echo stripos("helLo","l")."<br>";
echo 
stripos("helLo","L");
?>
Output
2
2

Thanks
__________________
K K Venkata Charya
Success is easy to get but difficult to retain
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #62 (permalink)  
Old 11-11-2007, 09:37 PM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: string functions and manipulations in php

Hi

Strstr Vs Stristr


The strstr() function searches for the first occurrence of a string inside another string.This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.This function is case-sensitive.

The stristr() function searches for the first occurrence of a string inside another string.This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.This function is case-insensitive.


PHP Code:
<?php
echo strstr("Hello world!","world");
?>

PHP Code:
<?php
echo stristr("Hello world!","WORLD");
?>
Ouput in both case is world!

Thanks
__________________
K K Venkata Charya
Success is easy to get but difficult to retain
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #63 (permalink)  
Old 11-11-2007, 10:01 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Wink Re: string functions and manipulations in php

What is chunk_split and how it differ from split function?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #64 (permalink)  
Old 11-11-2007, 10:04 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Wink Re: string functions and manipulations in php

Can anybody explain about str_shuffle and use of it?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #65 (permalink)  
Old 11-11-2007, 10:07 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Wink Re: string functions and manipulations in php

Explain about strchr() and strrchr()?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #66 (permalink)  
Old 11-11-2007, 10:34 PM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: string functions and manipulations in php

Hi

chunk_split() function splits a string into a series of smaller part. But split() function split a string into array by regular expression. chunk_split() function does not alter the original string but split() function alter the string into array.

The below is the details about the use of chunk_split() and split() functions

chunk_split()

chunk_split() function splits a string into a series of smaller parts.

Syntax
chunk_split(string,length,end)

Parameter Description

string Required. Specifies the string to split
length Optional. A number that defines the length of the chunks. Default is 76
end Optional. A string that defines what to place at the end of each chunk. Default is \r\n


Example

In this example we will split the string after each character and add a "." after each split:

<?php
$str = "Hello world!";
echo chunk_split($str,1,".");
?>

The output of the code above will be:

H.e.l.l.o. .w.o.r.l.d.!.


Example
In this example we will split the string after the sixth character and add "..." after each split:

<?php
$str = "Hello world!";
echo chunk_split($str,6,"...");
?>

The output of the code above will be:

Hello ...world!...


split()

split() function Split string into array by regular expression

split (string pattern, string string [, int limit])

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the case-sensitive regular expression pattern. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the whole rest of string. If an error occurs, split() returns FALSE.

Example:

<?php
// Delimiters may be slash, dot, or hyphen
$date = "04/30/1973";
list($month, $day, $year) = split('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br />\n";
?>

Last edited by Senthilkumar : 11-11-2007 at 10:42 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #67 (permalink)  
Old 11-11-2007, 11:16 PM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: string functions and manipulations in php

str_shuffle()

Definition and Usage

str_shuffle() function is used to shuffles all the characters of a string randomly. One permutation of all possible is created.

Syntax
str_shuffle(string)

Parameter Description
string Required. Specifies the string to shuffle


Example
<?php
echo str_shuffle("Hello World");
?>

The output of the code above could be:

H leooWlrld
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #68 (permalink)  
Old 11-11-2007, 11:37 PM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: string functions and manipulations in php

strchr()

Definition and Usage

strchr() function searches for the first occurrence of a string inside another string.

This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.

This function is an alias of the strstr() function.

Syntax
strchr(string,search)

Parameter Description
string Required. Specifies the string to search
search Required. Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number


Note: This function is a binary safe and case-sensitive function.


Example
<?php
echo strchr("Hello world!","world");
?>

The output of the code above will be:

world!


Example
In this example we will search a string for the ASCII value of "o":

<?php
echo strchr("Hello world!",111);
?>

The output of the code above will be:

o world!



strrchr()

Definition and Usage
strrchr() function finds the position of the last occurrence of a string within another string, and returns all characters from this position to the end of the string.

If char cannot be found, this function returns FALSE.

Syntax
strrchr(string,char)

Parameter Description
string Required. Specifies the string to search
char Required. Specifies the string to find. If this is a number, it will search for the character matching the ASCII value of that number


Note: This function is binary safe.


Example
<?php
echo strrchr("Hello world!","world");
?>

The output of the code above will be:

world!


Example
In this example we will search a string for the ASCII value of "o":

<?php
echo strrchr("Hello world!",111);
?>

The output of the code above will be:

orld!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #69 (permalink)  
Old 11-12-2007, 09:58 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Wink Re: string functions and manipulations in php

Explain the types of string compare functions in php?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #70 (permalink)  
Old 11-12-2007, 10:00 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Wink Re: string functions and manipulations in php

What is the difference between the chop() and rtrim()?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #71 (permalink)  
Old 11-13-2007, 06:04 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: string functions and manipulations in php

Hi vims,

strcmp — Binary safe string comparison
int strcmp ( string $str1, string $str2 )

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Note: This comparison is case sensitive.
Code:
$str1 = "Test";
$str2 = "Test";
$str3 = "Apple";
$str4 = "Zebra";
if (strcmp($str1,$str2) == 0) echo "Equal";
if (strcmp($str1,"Test") == 0) echo "Equal";
if (strcmp($str1,$str3) > 0) echo "$str1 > $str3";
if (strcmp($str1,$str4) < 0) echo "$str1 < $str4";
Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #72 (permalink)  
Old 11-13-2007, 06:11 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: string functions and manipulations in php

Hi,

strcasecmp - Binary safe case-insensitive string comparison
int strcasecmp ( string $str1, string $str2 )

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Note: Binary safe case-insensitive string comparison.
Code:
<?php
$var1 = "Test";
$var2 = "test";
if (strcasecmp($var1, $var2) == 0) {
    echo '$var1 is equal to $var2 in a case-insensitive string comparison';
}
?>
Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #73 (permalink)  
Old 11-14-2007, 09:15 PM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: string functions and manipulations in php

Quote:
Originally Posted by vims View Post
What is the difference between the chop() and rtrim()?
Hi,

I do not find much difference between these two but in my practical work i found these two have a difference in performance.

rtrim is much faster than chop.

Both rtrim and chop is used to remove a white space or other predefined character from the right end of a string where chop is an alias of the rtrim function.

rtrim(string,charlist)
chop(string,charlist)

Example of chop function
PHP Code:
<?php
$str 
"Hello World!\n\n";
echo 
$str;
echo 
chop($str);
?>
Example of chop function
PHP Code:
<?php
$str 
"Hello World!    ";
echo 
"Without rtrim: " $str;
echo 
"<br />";
echo 
"With rtrim: " rtrim($str);
?>
Thanks
__________________
K K Venkata Charya
Success is easy to get but difficult to retain
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #74 (permalink)  
Old 11-15-2007, 08:19 AM
venkatbi venkatbi is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 80
venkatbi is on a distinguished road
Default Re: string functions and manipulations in php

substr_compare():
Binary safe comparison of 2 strings from an offset, up to length characters
examples:
echo substr_compare("abcde", "bc", 1, 2); //here 1 is the offset and 2 is length of comparison
echo substr_compare("abcde", "de", -2, 2);
__________________
.......................
Thanks,
Venkatbi...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #75 (permalink)  
Old 11-15-2007, 08:27 PM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: string functions and manipulations in php

Hi,

Here i am discussing about uuencode and uudecode and its convert function.

Uudecode Vs Uuencode

A set of algorithms for converting files into a series of 7-bit ASCII characters that can be transmitted over the Internet. Originally, uuencode stood for Unix-to-Unix encode and uudecode for Unix-to-Unix decode but it has since become a universal protocol used to transfer files between different platforms such as Unix, Windows, and Macintosh. Uuencoding is especially popular for sending e-mail attachments. Nearly all e-mail applications support uuencoding for sending attachments and uudecoding for receiving attachments.

It is used in string manipulation to convert the value as encode to decode or vice versa.

PHP Code:
<?php
$str 
",2&5L;&\@=V]R;&0A `";
echo 
convert_uudecode($str);
?>
Output
Hello world!

<?php
$str = "Hello world!";
echo convert_uuencode($str);
?>

Output
,2&5L;&\@=V]R;&0A `

Thanks

__________________
K K Venkata Charya
Success is easy to get but difficult to retain
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #76 (permalink)  
Old 11-15-2007, 08:41 PM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Thumbs up Re: string functions and manipulations in php

Hi

htmlspecialchars() Vs htmlentities()

The htmlspecialchars() function converts some predefined characters to HTML entities.

The predefined characters are:
& (ampersand) becomes &amp;
" (double quote) becomes &quot;
' (single quote) becomes '
< (less than) becomes &lt;
> (greater than) becomes &gt;

Syntax
string htmlspecialchars ( string string [, int quote_style [, string charset]] )

The htmlentities() function converts characters to HTML entities.

Syntax
string htmlentities ( string string [, int quote_style [, string charset]] )

Difference between htmlspecialchars and htmlentities
htmlspecialchars only takes care of <, >, single quote, double quote and ampersand whereas htmlentities translates all occurrences of character sequences that have some other meaning in HTML. Also htmlentities( ) will check for non English language characters, such as French accents, the German umlaut, etc.

PHP Code:
<?php
$str 
"Jane & 'Tarzan'";
echo 
htmlspecialchars($strENT_COMPAT);
Ouput
Jane & 'Tarzan'


PHP Code:
<?php
$str 
"My name is Øyvind Åsane. I'm Norwegian";
echo 
htmlentities($strENT_COMPAT"ISO-8859-1");
?>
Output
My name is Øyvind Åsane. I'm Norwegian

Thanks
__________________
K K Venkata Charya
Success is easy to get but difficult to retain
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #77 (permalink)  
Old 11-15-2007, 09:58 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Wink Re: string functions and manipulations in php

strcspn

strcspn — Returns the number of characters present in string before any part of mask is found.

Syntax:
int strcspn(string, mask);
string string: String to search
string mask: List of characters to find

Description:
strcspn() returns the number of characters that occur between the start of string and the first occurrence of any character listed in mask .

strcspn() provides a simple way to parse strings and validate data. It is not commonly used in PHP - many other functions exist in the language that eclipse it in power and convenience.

Example:
Display names that don't start with a vowel

<?php
$list = array("Andrew", "Brigitte", "Chris", "Deb");
foreach($list as $name) {
if(strcspn($name, 'aeiouyAEIOUY')) {
echo $name, "\n";
}
}
?>

Output:
Brigitte
Chris
Deb
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #78 (permalink)  
Old 11-15-2007, 10:01 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Wink Re: string functions and manipulations in php

strspn

strspn — Finds the length of the initial substring containing only characters from mask .

Syntax:
int strspn(string, mask);
string string: String to search
string mask: Characters to find

Description:
strspn() returns the length of the substring at the start of string that contains only characters from mask . Given string abcdefand mask abc, for example, the function returns3.

Example:
Display names that start with vowels

<?php
$list = array("Andrew", "Brigitte", "Chris", "Deb");
foreach($list as $name) {
if (strspn($name, 'aeiouyAEIOUY')) {
echo $name, "\n";
}
}
?>

Output:
Andrew
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #79 (permalink)  
Old 11-15-2007, 10:09 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Wink Re: string functions and manipulations in php

similar_text

similar_text — Calculates the similarity of two strings.

Syntax:
int similar_text(string_one, string_two[, $percent]);
string string_one: First string to be compared
string string_two: Second string to be compared
variable $percent (optional): Variable to store the percentage similarity of the strings

Description:
similar_text() calculates the similarity of two strings. The function returns the number of unique characters that appear in both strings. The function can store the percentage similarity of the strings in the optional $percent parameter.

Example:
A cheesy example

<?php
$term = 'cheese';
foreach(array('gouda', 'gruyere', 'cheddar') as $match) {
echo similar_text($term, $match, $percent),
" characters from '$term' were contained in '$match'.\n",
"Overall, '$term' is a ", round($percent),
"% match for '$match'\n\n";
}
?>

Output:
0 characters from 'cheese' were contained in 'gouda'.
Overall, 'cheese' is a 0% match for 'gouda'

2 characters from 'cheese' were contained in 'gruyere'.
Overall, 'cheese' is a 31% match for 'gruyere'

3 characters from 'cheese' were contained in 'cheddar'.
Overall, 'cheese' is a 46% match for 'cheddar'
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #80 (permalink)  
Old 11-15-2007, 10:20 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Wink Re: string functions and manipulations in php

sscanf

sscanf — Parses input from a string according to a specified format.

Syntax:
mixed sscanf(string, format[, $variables]);
string string: String to parse
string format: Format to use
mixed $variables (optional): One or more variables to store

Description:
sscanf() parses string into variables based on the format string. however, instead of formatting and transforming variables, it provides a template with which to parse a string into variables.

This function accepts two or more arguments. If only two arguments are provided, the data parsed from string are returned as a numerically keyed array. If additional arguments are passed to the function, the data parsed out of string are stored in them. If there are more specifiers than variables to contain them, an error is generated. If the opposite is true, the extra variables contain NULL .

Example:
Parse data out of a simple formatted string

<?php
$string = 'age:27 height:1.83m weight:90kg';
sscanf($string, 'age:%d height:%fm weight:%dkg', $age, $height, $weight);

// use var_dump to show the types, as well as the values
var_dump($age, $height, $weight);
?>