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; In this thread we will discuss in detail about various string functions and manipulations used in php. Let us start ...


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

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-05-2007, 06:20 AM
Anand Anand is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 52
Anand is on a distinguished road
Default string functions and manipulations in php

In this thread we will discuss in detail about various string functions and manipulations used in php. Let us start with the trim function in php.

trim -- Strip whitespace from the beginning and end of a string.

Syntax : trim(string,charlist)

string - Specifies the string to check

charlist - Specifies which characters to remove from the string. If omitted, all of the following characters are removed:
* "\0" - NULL
* "\t" - tab
* "\n" - new line
* "\x0B" - vertical tab
* "\r" - carriage return
* " " - ordinary white space
__________________
None of us is As Strong as All of us.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-06-2007, 03:25 AM
varghese varghese is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 93
varghese is on a distinguished road
Default Re: string functions and manipulations in php

Hi ,
How to convert a string in to array?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-06-2007, 05:35 AM
Sivamurugan Sivamurugan is offline
D-Web Trainee
 
Join Date: Mar 2007
Posts: 22
Sivamurugan is on a distinguished road
Default Re: string functions and manipulations in php

Quote:
Originally Posted by Anand View Post
In this thread we will discuss in detail about various string functions and manipulations used in php. Let us start with the trim function in php.

trim -- Strip whitespace from the beginning and end of a string.

Syntax : trim(string,charlist)

string - Specifies the string to check

charlist - Specifies which characters to remove from the string. If omitted, all of the following characters are removed:
* "\0" - NULL
* "\t" - tab
* "\n" - new line
* "\x0B" - vertical tab
* "\r" - carriage return
* " " - ordinary white space




Anand

How to use trim for right side also for left side spaces
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-07-2007, 09:29 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 varghese View Post
Hi ,
How to convert a string in to array?
Hi varghese,

One very simple way to convert any string as array is using "explode" function.

For example if your string like that ->

$vStrVal = "one,two,three,four";
$aArrVal = explode(",",$vStrVal);


It is most commonly used way to do that.

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
  #5 (permalink)  
Old 10-07-2007, 09:38 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Post Re: string functions and manipulations in php

Quote:
Originally Posted by Sivamurugan View Post
Anand

How to use trim for right side also for left side spaces
ltrim - trim used for removing the left side spaces

It will remove the whitespaces on the left side that is it will remove the whitespaces from the beginning of the string.

Let us explain with this example

$str = " have a nice day";
echo ltrim($str);
output : have a nice day (without white spaces / left side spaces)

rtrim - trim used for removing the right side spaces

It will remove the whitespaces on the right side that is it will remove the whitespaces from the end of the string

Let us explain with this example

$str = "Good Morning ";
echo rtrim($str);
output : Good Morning (without white spaces / right side spaces)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 10-07-2007, 09:41 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Default Re: string functions and manipulations in php

What is the difference between the strstr() and stristr()?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-07-2007, 09:57 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Post Re: string functions and manipulations in php

Let us discuss about the string function called str_replace()

str_replace() function replaces some characters with some other characters in a string.

This function works by the following rules:
  • If the string to be searched is an array, it returns an array
  • If the string to be searched is an array, find and replace is performed with every array element
  • If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace
  • If find is an array and replace is a string, the replace string will be used for every find value

Syntax
str_replace(find,replace,string,count)

Parameter Description
find - Required Specifies the value to find
replace - Required. Specifies the value to replace the value in find
string - Required. Specifies the string to be searched
count - Optional. A variable that counts the number of replacements

Example
<?php
echo str_replace("Evening","Morning","Good Evening");
?>

Output : Good Morning
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 10-07-2007, 10:18 PM
varghese varghese is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 93
varghese is on a distinguished road
Thumbs up Re: string functions and manipulations in php

Quote:
Originally Posted by venkat_charya View Post
Hi varghese,

One very simple way to convert any string as array is using "explode" function.

For example if your string like that ->

$vStrVal = "one,two,three,four";
$aArrVal = explode(",",$vStrVal);


It is most commonly used way to do that.

Thanks
Hi Venkat_charya,
In explode we have to use any key word but i need to convert a array as each character without any key word.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 10-07-2007, 11:43 PM
Anand Anand is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 52
Anand is on a distinguished road
Default Re: string functions and manipulations in php

Varghese,

Like "C language" we can directly refer to the variable with the array.

For example,
$str = "string";
for($i=0;$i<strlen($str);$i++)
echo $str[$i];

The above code will print "string". You can directly refer to the characters with the positions.
__________________
None of us is As Strong as All of us.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 10-08-2007, 11:45 AM
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 strstr() and stristr()?

strstr is a conceptual integration of strpos above: whereas strpos locates a match and returns the number of its index offset, strstr still locates the first match but doesn't return the number but the whole string from there:
$string='hallo world hallo' ;
print strstr($string,'hallo');

stristr is the same as strstr, but this round case insensitive.


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
  #11 (permalink)  
Old 10-08-2007, 09:31 PM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Post Re: string functions and manipulations in php

Let us discuss about the substr()

Substr() - Returns a part of the string

Syntax :
string substr ( string string, int start [, int length] )

Parameters
string - specifies the string
start - specifies the start index
length - specifies the length of the substring

Example :
echo substr("happy birthday",0, 5)
Output: happy

If you not specified the length parameter (parameter 3), returns the string from $start to the end of the string.

Example:
echo substr("happy birthday",6);
Output: birthday

If the $start parameter is a negative number, the position starts from end of the string.

Example:
echo substr("happy birthday",-8);
Output: birthday

echo substr("happy birthday",-8, 5);
Output: birth

If $length is negative, substr( ) counts back from the end of the string to
determine where your substring ends:

Example:
echo substr(’on the way to operating theatre’, 4,-3)
Output: he way to operating thea
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 10-09-2007, 03:03 AM
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 Anand

In PHP 5 one function available to split string without loop

<?php

$str = "Hello Friend";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);

?>

output:

Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)

Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 10-09-2007, 03:29 AM
varghese varghese is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 93
varghese is on a distinguished road
Default Re: string functions and manipulations in php

I have a question about how to manipulate php string.
For example, I have a text string:
$vString="
---<updated:12/20/06>---
Updated First
---<updated:12/19/06>---
Updated second";

Now if I want to remove "---<updated:XX/XX/XX>---" and only display:
Updated First
Updated second

how can I do that? Is there a php function can do this?

Thanks
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 10-10-2007, 04:40 AM
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 Varghese check this,
ereg_replace("(---<updated: )[0-9]{2}(/)[0-9]{2}(/)[0-9]{2}(>---)","",$vString);

Last edited by Senthilkumar : 10-10-2007 at 04:42 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 10-10-2007, 04:44 AM
varghese varghese is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 93
varghese is on a distinguished road
Thumbs up Re: string functions and manipulations in php

Thanks Senthil It working fine.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 10-12-2007, 04:49 AM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Post Re: string functions and manipulations in php

Today we will discuss about the strip_tags()

Strip_tags() - It will strip the html and php contents from the given string

Syntax:
string strip_tags ( string $str [, string $allowable_tags] )

Parameters
str
The input string.
allowable_tags
You can use the optional second parameter to specify tags which should not be stripped.
Note: HTML comments are also stripped. This is hardcoded and can not be changed with allowable_tags.

Example
echo strip_tags("Fantastic <b>day!</b>");
Output:
Fantasic day!
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text, '<p><a>');
Output:
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 10-12-2007, 05:04 AM
vims vims is offline
D-Web Programmer
 
Join Date: Oct 2007
Posts: 67
vims is on a distinguished road
Default Re: string functions and manipulations in php

Now discuss about addcslashes()

addcslashes() - Returns a string with backslashes in front of the specified characters.

Syntax
addcslashes(string,characters)

Example
$str = "Hello, my name is Kai Jim.";
echo $str."<br />";
echo addcslashes($str,'A..Z')."<br />";
echo addcslashes($str,'a..z')."<br />";
echo addcslashes($str,'a..h');

Output:
Hello, my name is Kai Jim.
\Hello, my name is \Kai \Jim.
H\e\l\l\o, \m\y \n\a\m\e \i\s K\a\i J\i\m.
H\ello, my n\am\e is K\ai Jim.

Note:
Be careful using addcslashes() on 0, r, n and t. In PHP, \0, \r, \n and \t are predefined escape sequences.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 10-13-2007, 02:12 AM
varghese varghese is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 93
varghese is on a distinguished road
Default Re: string functions and manipulations in php

What is pack() and unpack() in php?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 10-14-2007, 08:46 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 Varghese,

These functions let programmer directly manipulate binary data. The distinction between signed and unsigned values only affects the function unpack(), where as function pack() gives the same result for signed and unsigned format codes.

There are some format values available for both Pack and Unpack function. Those possible values are:

a - NUL-padded string
A - SPACE-padded string
h - Hex string, low nibble first
H - Hex string, high nibble first
c - signed char
C - unsigned char
s - signed short (always 16 bit, machine byte order)
S - unsigned short (always 16 bit, machine byte order)
n - unsigned short (always 16 bit, big endian byte order)
v - unsigned short (always 16 bit, little endian byte order)
i - signed integer (machine dependent size and byte order)
I - unsigned integer (machine dependent size and byte order)
l - signed long (always 32 bit, machine byte order)
L - unsigned long (always 32 bit, machine byte order)
N - unsigned long (always 32 bit, big endian byte order)
V - unsigned long (always 32 bit, little endian byte order)
f - float (machine dependent size and representation)
d - double (machine dependent size and representation)
x - NUL byte
X - Back up one byte
@ - NUL-fill to absolute position


Syntax for pack:
pack(format,args+)

Syntax for unpack:
unpack(format,data)
This function returns an array on success, or FALSE on failure.

Pack Example:

<?php
echo pack("C3",80,72,80);
?>
The output of the code above will be:
PHP


Another one is
<?php
echo pack("C*",80,72,80);
?>
The output of the code above will be:
PHP



Unpack example:
<?php
$data = "PHP";
print_r(unpack("C*",$data));
?>

The output of the code above will be:

Array
(
[1] => 80
[2] => 72
[3] => 80
)


I hope it clear your doubt.

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
  #20 (permalink)  
Old 10-14-2007, 09:09 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

As like addcslashes there is another string function available called addslashes().

It returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).


For Example:
<?php
$str = "Is your name O'reilly?";
// Outputs: Is your name O\'reilly?
echo addslashes($str);
?>


Another Example
<?php
$str = "Who's Kai Jim?";
echo $str . " This is not safe in a database query.<br />";
echo addslashes($str) . " This is safe in a database query.";
?>
Ouput:
Who's Kai Jim? This is not safe in a database query.
Who\'s Kai Jim? This is safe in a database query.


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
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare a string with String Array. oxygen C# Programming 5 12-28-2007 10:18 PM
What is diffrenece between string.compare and string.compareordinal shaalini ASP and ASP.NET Programming 3 12-28-2007 09:46 PM
What are Virtual Functions? How to implement virtual functions in "C"? Sabari C and C++ Programming 4 09-10-2007 10:35 PM
TSL functions used for moving the pointer to that text string in winrunner senthilkannan Testing Tools 0 07-30-2007 02:08 AM
How to split a string by using a string usually in the c# dotnet... Archer C# Programming 1 07-25-2007 02:26 AM