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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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 |
| Sponsored Links |
| |||
| 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 |
| |||
| 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. |
| |||
| 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 |
| |||
| 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 |
| |||
| 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'; |
| |||
| 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 |
| |||
| 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. |
| |||
| Quote:
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. |
| |||
| Quote:
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 |
| |||
| 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 <b>bold</b> echo htmlentities($str); // Outputs: A 'quote' is <b>bold</b> echo htmlentities($str, ENT_QUOTES); ?> Output A 'quote' is bold Thanks
__________________ K K Venkata Charya Success is easy to get but difficult to retain |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 webecho ucfirst(strtolower("DISCUSS WEB")) Output: Discuss web |
| |||
| 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 Functionsecho ucwords(strtolower("STRING FUNCTIONS")) Output: String Functions |
| |||
| 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: stringIf 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 |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 |