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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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. |
| Sponsored Links |
| |||
| Quote:
Anand How to use trim for right side also for left side spaces |
| |||
| 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 |
| |||
| Quote:
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) |
| |||
| 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:
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 |
| |||
| Quote:
In explode we have to use any key word but i need to convert a array as each character without any key word. |
| |||
| 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. |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 ) |
| |||
| 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 |
| |||
| 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> |
| |||
| 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. |
| |||
| 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 |
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |