String Functions in PHP
String Functions in PHP
Functions in PHP
string functions in PHP
• strlen ()
• strrev()
• str_word_count()
• strtoupper()
• strtolower()
• implode()
• explode()
• substr(string,start,length)
• <p> implode function - array to a string </p>
• <?php
• $arr = array('Java','C++','CSS','VB');
• echo implode(" ",$arr);
• ?>
• <p> Explode function - string to an array </p>
• <?php
• $str = "Hello world. It's a beautiful day.";
• print_r (explode(" ",$str));
• ?>
File handling
• file_put_contents(filename, data, mode, context)
• - filename and data are compulsary and remaining
parameters are optional.
• file_put_contents("test.txt","Hello World - IWP.
Testing!");
• Return value -> The number of bytes written into the file on
success, FALSE on failure
• file_get_contents function in php reads the file
content into a string.
• file_get_contents(path, include_path, context, star
t, max_length) -> path compulsory and remaining
parameters are optional.
• echo file_get_contents("test.txt");
Date Functions – oops in php
<?php
$l = date_create($_GET["birthday"]); // read from form input type is date
from html using GET method.
$d = date_create("2022-04-30");// date_create() returns DateTime object
$df = date_diff($l,$d); // date_diff()// returns the difference between two DateTime
object as DateInterval object
• <?php
• $t=time();//The time() function returns the current time in the number of seconds since the January 1 1970 00:00:00 GMT
• echo($t . "<br>");
• echo(date("Y-m-d",$t));// date() returns the date in the year-month-date format from $t.
• ?>
• </body>
• </html>
Format method
• DateInterval::format(format $string): String -> (will be discussed in
detail while discussing functions in php)
• Next slide has the list of characters for getting the output as per our
requirement.
Required. Specifies the format. The following characters can be used in
the format parameter string:% - Literal %
•Y - Year, at least 2 digits with leading zero (e.g 03)
•y - Year (e.g 3)
•M - Month, with leading zero (e.g 06)
•m - Month (e.g 6)
•D - Day, with leading zero (e.g 09)
•d - Day (e.g 9)
•a - Total number of days as a result of date_diff()
•H - Hours, with leading zero (e.g 08, 23)
•h - Hours (e.g 8, 23)
•I - Minutes, with leading zero (e.g 08, 23)
•i - Minutes (e.g 8, 23)
•S - Seconds, with leading zero (e.g 08, 23)
•s - Seconds (e.g 8, 23)
•F - Microseconds, at least 6 digits (e.g 004403, 235689)
•f - Microseconds (e.g 4403, 235689)
•R - Sign "-" when negative, "+" when positive
•r - Sign "-" when negative, empty when positive
•Note: Each format character must be prefixed by a % sign!
Reference
• W3.schools.com