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 As like we using addslashes to add slashes in string, we can use another string function to remove slashes ...


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

Register FAQ Members List Calendar Mark Forums Read
  #21 (permalink)  
Old 10-16-2007, 03:33 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

hi

As like we using addslashes to add slashes in string, we can use another string function to remove slashes in string. That function is called stripslashes

Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).

<?php
$str = "Is your name O\'reilly?";

// Outputs: Is your name O'reilly?
echo stripslashes($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
Sponsored Links
  #22 (permalink)  
Old 10-16-2007, 03:41 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

hi,

As like addcslashes to add specific escape sequence in string there is another function which do vice versa and remove those in any string. That function is called stripcslashes.

It returns a string with backslashes stripped off. Recognizes C-like \n, \r ..., octal and hexadecimal representation.

<?php
echo stripcslashes("Hello, \my na\me is Kai Ji\m.");
?>


The output of the code above will be:
Hello, my name is Kai Jim.


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
  #23 (permalink)  
Old 10-16-2007, 04:08 AM
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

Hi,

I have one basic doubt. How can we show the PHP codes and HTML tags to show up in a browser, like the one which we show in the DiscussWeb forum. I would like to read a php page and show all the contents in the browser.
__________________
None of us is As Strong as All of us.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #24 (permalink)  
Old 10-16-2007, 04:09 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 are going to discuss about the strtotime()
Strtotime() - Parse about any English textual datetime description into a Unix timestamp

Syntax
int strtotime ( string time [, int now] )
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT), relative to the timestamp given in now, or the current time if none is supplied. Upon failure, -1 is returned.

Example:
echo strtotime("now")
Output: 1192529546

echo date('l, F jS Y', strtotime("third wednesday", strtotime("2007-10-01"))) . "<br>";
Output: Wednesday, October 17th 2007

echo date('l, F jS Y', strtotime("third sunday", strtotime("2007-10-01")));
Output: Sunday, October 21st 2007
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #25 (permalink)  
Old 10-16-2007, 04:20 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

Let us discuss about the function is_array()

is_array() - Check whether the given variable is an array

Syntax:
bool is_array ( mixed var )
Example:
$arrColor = array('Red', 'Blue', 'Green');
echo is_array($arrColor) ? 'It is an array' : 'It is not an array';

Output: It is an array
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #26 (permalink)  
Old 10-16-2007, 04:28 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

Let us discuss about is_int() and is_float()

is_int() - Check whether the given variable is an integer

Syntax:
bool is_int ( mixed var )

Example:
echo is_int(5) ? 'Integer' : 'Not an Integer';

is_float() - Check whether the given variable is an float

Syntax:
bool is_float( mixed var )

Example:
echo is_float(4.6) ? 'Float Variable' : 'Not a Float Variable';

Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #27 (permalink)  
Old 10-17-2007, 03:19 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

hi,

Here we discussing about the replace function. In string functions of PHP there is one function called "str_replace()", which used to replace any single or set of charcter to anyother format.

Syntax
str_replace(find,replace,string,count)

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("world","Peter","Hello world!");
?>


The output of the code above will be:
Hello Peter!

For case sensitive scenario, we can use str_ireplace() function.


Syntax
str_ireplace(find,replace,string,count)

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

<?php
echo str_ireplace("WORLD","Peter","Hello world!");
?>

The output of the code above will be:
Hello Peter!


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
  #28 (permalink)  
Old 10-17-2007, 07:57 AM
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

Can anybody explain the differences between the echo and print functions. Also, under what circumstances we need to use these functions in php?.
__________________
None of us is As Strong as All of us.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #29 (permalink)  
Old 10-19-2007, 06:04 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

Quote:
Originally Posted by Anand View Post
Can anybody explain the differences between the echo and print functions. Also, under what circumstances we need to use these functions in php?.
There is a difference between the two, but speed-wise it should be
irrelevant which one you use.

print() behaves like a function in that you can do: $ret = print "Hello World";And $ret will be 1 That means that print can be used as part of a more complex expression where echo cannot.

print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precendence list though. Only "," AND, OR and XOR are lower.

echo is marginally faster since it doesn't set a return value.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #30 (permalink)  
Old 10-22-2007, 09:44 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: string functions and manipulations in php

hi,

example:
---------
$array = array('sen', 'senraj@gmail.coml', 'raj','senraj@yahoo.com');

how to convert an array to string?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #31 (permalink)  
Old 10-24-2007, 08:46 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 senraj View Post
hi,

example:
---------
$array = array('sen', 'senraj@gmail.coml', 'raj','senraj@yahoo.com');

how to convert an array to string?
Hi senraj,

We can use implode function to convert array as string.
$array = array('sen', 'senraj@gmail.coml', 'raj','senraj@yahoo.com');
$vArr = implode(",",$array);


output should display like that

sen,senraj@gmail.coml,raj,senraj@yahoo.com

I hope you got answer of 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
  #32 (permalink)  
Old 10-24-2007, 08:58 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

Hi Friends,

Here i am discussing about string functions called htmlentities.

The htmlentities() function converts characters to HTML entities.

Syntax
htmlentities(string,quotestyle,character-set)

Parameter Description
string:Specifies the string to convert. Required.
quotestyle: Specifies how to encode single and double quotes.Optional.
The available quote styles are:
ENT_COMPAT - Default. Encodes only double quotes
ENT_QUOTES - Encodes double and single quotes
ENT_NOQUOTES - Does not encode any quotes

character-set: A string that specifies which character-set to use.Optional.
Allowed values are:
ISO-8859-1 - Default. Western European
ISO-8859-15 - Western European (adds the Euro sign + French and Finnish letters missing in ISO-8859-1)
UTF-8 - ASCII compatible multi-byte 8-bit Unicode
cp866 - DOS-specific Cyrillic charset
cp1251 - Windows-specific Cyrillic charset
cp1252 - Windows specific charset for Western European
KOI8-R - Russian
BIG5 - Traditional Chinese, mainly used in Taiwan
GB2312 - Simplified Chinese, national standard character set
BIG5-HKSCS - Big5 with Hong Kong extensions
Shift_JIS - Japanese
EUC-JP - Japanese

Input
<?php
$str = "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str, ENT_QUOTES);
?>


Output
A 'quote' is bold


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
  #33 (permalink)  
Old 10-24-2007, 10:06 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

Let us discuss about the strtoupper()

Strtoupper() - Converts the whole alphabetic characters to uppercase

Syntax
string strtoupper ( string string)
Example
echo strtoupper("Happy diwali")
Output:
HAPPY DIWALI
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #34 (permalink)  
Old 10-24-2007, 10:11 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

Now let us discuss about the string function called strtolower()

strtolower() - Converts the entire alphabetic characters to lowercase

Syntax:
string strtolower ( string str)
Example:
echo strtolower("CELEBRATIONS")
Output:
celebrations
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #35 (permalink)  
Old 10-24-2007, 10:17 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

Now i am going to discuss about the functions ucfirst()

ucfirst() - Converts the first character of string to capitalized, if that character is alphabetic.

Syntax:
string ucfirst ( string str)
Example:
echo ucfirst("discuss web")
Output:
Discuss web
echo ucfirst(strtolower("DISCUSS WEB"))
Output:
Discuss web
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #36 (permalink)  
Old 10-24-2007, 10:21 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

Let us discuss about the string function ucwords

ucwords() - Converts the first character of each word of string to capitalized, if that character is alphabetic

Syntax:
string ucwords ( string str)
Example:
echo ucwords("string functions")
Output:
String Functions
echo ucwords(strtolower("STRING FUNCTIONS"))
Output:
String Functions
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #37 (permalink)  
Old 10-24-2007, 10:38 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

Let us explain about the string function called wordwrap()

wordwrap() - Wraps a string to a given number of characters using a string break character.

Syntax:
string wordwrap ( string str [, int width [, string break [, int cut]]])
String is wrapped at the column specified by the given width parameter. If the width parameter is not given it will wrapped automatically at column 75 using the newline character.

Example:
$vString = "string functions and manipulations in php";
echo wordwrap($vString, 7);
Output:
string
functions and maniputlations in php
If we specified the cut parameter as 1 then the entire string is wrapped at specified width parameter

Example:
$vString = "A very long woooooooooooord.";
echo wordwrap( $vString, 8, "\n", 1);
Output:
A very
long
wooooooo
ooooord.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #38 (permalink)  
Old 10-25-2007, 10:55 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 friends,

Here i am discussing about substr() function. This function returns the portion of string specified by the start and length parameters.

Syntax
substr(string,start,length)

Parameter
string = Specifies the string to return a part of. It is Required field

start = Specifies where to start in the string. It is Required field
A positive number - Start at a specified position in the string
A negative number - Start at a specified position from the end of the string
0 - Start at the first character in string

length = Specifies the length of the returned string. Default is to the end of the string. It is Optional field.
A positive number - The length to be returned from the start parameter
Negative number - The length to be returned from the end of the string

Example

<?php
echo substr('abcdef', 1); // bcdef
echo substr('abcdef', 1, 3); // bcd
echo substr('abcdef', 0, 4); // abcd
echo substr('abcdef', 0, 8); // abcdef
echo substr('abcdef', -1, 1); // f

// Accessing single characters in a string
// can also be achived using "curly braces"
$string = 'abcdef';
echo $string{0}; // a
echo $string{3}; // d
echo $string{strlen($string)-1}; // f

?>


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
  #39 (permalink)  
Old 10-25-2007, 11:02 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

There is another string function which i am going to discuss is substr_replace.

substr_replace() replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement. The result string is returned. If string is an array then array is returned.

Syntax
substr_replace(string,replacement,start,length)

Parameter
string = Specifies the string to check.It's required field.
replacement = Specifies the string to insert.It's required field.
start = Specifies where to start replacing in the string. It's required field.
A positive number - Start replacing at the specified position in the string.
Negative number - Start replacing at the specified position from the end of the string
0 - Start replacing at the first character in the string
length = Specifies how many characters should be replaced. Default is the same length as the string.It's optional field.
A positive number - The length of string to be replaced
A negative number - How many characters should be left at end of string after replacing
0 - Insert instead of replace

Example
<?php
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original: $var<hr />\n";

/* These two examples replace all of $var with 'bob'. */
echo substr_replace($var, 'bob', 0) . "<br />\n";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n";

/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var, 'bob', 0, 0) . "<br />\n";

/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
echo substr_replace($var, 'bob', -7, -1) . "<br />\n";

/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', 10, -1) . "<br />\n";
?>


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
  #40 (permalink)  
Old 10-25-2007, 11: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

While I am discussing about string function substr there are few more functions related to substr. One of the functions is called substr_count

The substr_count() function counts the number of times a substring occurs in a string.

Syntax
substr_count(string,substring,start,length)

Parameter Description
string = Specifies the string to check.It's required field.
substring = Specifies the string to search for. It's required field
start = Specifies where in string to start searching. It's optional field.
length = Specifies the length of the search. It's optional field.

Example
<?php
$text = 'This is a test';
echo strlen($text); // 14
echo substr_count($text, 'is'); // 2
// the string is reduced to 's is a test', so it prints 1
echo substr_count($text, 'is', 3);
// the text is reduced to 's i', so it prints 0
echo substr_count($text, 'is', 3, 3);
// generates a warning because 5+10 > 14
echo substr_count($text, 'is', 5, 10);
// prints only 1, because it doesn't count overlapped subtrings
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');
?>


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