String:: 3. String Manipulations and File Handling
String:: 3. String Manipulations and File Handling
String:
Strings can be seen as a stream of characters. It is a sequence of letters, numbers, special characters and
arithmetic values or combination of all.
For specifying a literal single quote, escape it with a backslash (\) and to specify a literal backslash (\) use
double backslash (\\).
<?php
$str='Hello text within single quote'; echo $str;
?>
Output:
Hello text within single quote
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
<?php
$str1='Hello text multiple line
text within single quoted string';
$str2='Using double "quote" directly inside single quoted string';
$str3='Using escape sequences \n in single quoted string';
echo "$str1 <br/> $str2 <br/> $str3";
?>
Output:
Hello text multiple line text within single quoted string
Using double "quote" directly inside single quoted string
Using escape sequences \n in single quoted string
3. String Manipulations And File Handling Centre for Distance and Online
Web Programming
Double quote:
In PHP, we can specify string through enclosing text within double quote also. But escape sequences and
variables will be interpreted using double quote PHP strings.
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
<?php
$num1=10;
echo "Number is: $num1";
?>
Output:
Number is: 10
Heredoc syntax
Heredoc syntax (<<<) is the third way to delimit strings. In Heredoc syntax, an identifier is provided after this heredoc
<<< operator, and immediately a new line is started to write any text. To close the quotation, the string follows itself
and then again that same identifier is provided. That closing identifier must begin from the new line without any
whitespace or tab.
Naming Rules
The identifier should follow the naming rule that it must contain only alphanumeric characters and underscores, and
must start with an underscore or a non-digit character.
<?php
$str = <<<Demo It is a valid example
Demo; //Valid code as whitespace or tab is not valid before closing identifier echo $str;
?>
Output:
It is a valid example
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
chop() It removes whitespace or other cha racters from the right end of a string
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Output:
mY name IS RAJ
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Where, format is Required parameter and Specifies the format of the timestamp.
timestamp is Optional and Specifies a timestamp. Default is the current date and time. A timestamp is a sequence of
characters, denoting the date and/or time at which a certain event occurred.
Get a Date
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
d - Represents the day of the month (01 to 31)
m - Represents a month (01 to 12)
Y - Represents a year (in four digits)
l (lowercase 'L') - Represents the day of the week
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Output:
Today is 2020/09/17
Today is 2020.09.17
Today is 2020-09-17
Today is Thursday
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Get a Time
Here are some characters that are commonly used for times:
H - 24-hour format of an hour (00 to 23)
h - 12-hour format of an hour with leading zeros (01 to 12)
i - Minutes with leading zeros (00 to 59)
s - Seconds with leading zeros (00 to 59)
a - Lowercase Ante meridiem and Post meridiem (am or pm) The example below outputs the current time in the
specified format:
Example: current time in the specified format
<html>
<body>
<?php
echo "The time is " . date("h:i:sa");
?>
</body>
</html>
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Syntax
mktime(hour, minute, second, month, day, year)
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
The example below creates a date and time from the strtotime() function:
Example : strtotime()
<html>
<body>
<?php
$d=strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
</body>
</html>
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
$d=strtotime("next Saturday");
echo "next Saturday is ",date("d-m-Y", $d) . "<br>";
$d=strtotime("+3 Months");
echo "date after 3 Months is ", date("d-m-Y", $d) . "<br>";
?>
</body>
</html>
Output:
tomorrow is 18-09-2020
next Saturday is 19-09-2020
date after 3 Months is 17-12-2020
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Function Description
checkdate() Validates a Gregorian date
date_add() Adds days, months, years, hours, minutes, and
seconds to a date
date_diff() Returns the difference between two dates
date_format() Returns a date formatted according to a
specified Format
date() Formats a local date and time
getdate() Returns date/time information of a timestamp or
the current local date/time
gettimeofday() Returns the current time
gmdate() Formats a GMT/UTC date and time
gmmktime() Returns the Unix timestamp for a GMT date
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Get request is the default form request. The data passed through get request is visible on the URL browser so it is not
secured. You can send limited amount of data through get request.
//form1.html
<form action="welcome.php" method="get"> Name: <input type="text" name="name"/>
<input type="submit" value="visit"/>
</form>
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file
welcome.php
<?php
$name=$_GET["name"];//receiving name field value in $name variable echo "Welcome, $name";
?>
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Post request is widely used to submit form that have large amount of data such as file upload, image upload, login
form, registration form etc.
The data passed through post request is not visible on the URL browser so it is secured. You can send large amount
of data through post request.
//form1.html
<form action="login.php" method="post">
<table>
<tr><td>Name:</td><td> <input type="text" name="name"/></td></tr>
<tr><td>Password:</td><td>
< i n p u t t y p e = " p a s s w o r d " name="password"/></td></tr>
<tr><td colspan="2"><input type="submit" value="login"/> </td></tr>
</table>
</form>
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file
login.php
<html>
<body>
<form action="welcome.php" method="post"> Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file
named "welcome.php". The form data is sent with the HTTP POST method.
To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this:
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Modes Description
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only . Erases the contents of the file or creates a new file if
It doesn't exist. File pointer starts at the beginning of the file
a Open a file for write only . The existing data in file is preserved. File pointer s
tarts at the end of the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already
Exists
r+
Open a file for read/write. File pointer starts at the beginning of the file
w+
Open a f ile for read/write . Erases the contents of the file or creates a new file
if
It doesn't exist. File pointer starts at the beginning of the file
a+
Open a file for read/write. The existing data in file is preserved. File pointer
starts at the end of the file. Creates a new file if the file doesn't exist
x+
Creates a new file for read/write. Returns FALSE and an error if file already
exists
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Example:fopen()
<?php
$handle = fopen("D:\VNM\Files\file.txt", "r");
?>
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Example: fread()
<?php
$filename = "D:\VNM\Files\file.txt";
$handle = fopen($filename, "r");//open file in read mode
$contents = fread($handle, filesize($filename));//read file
echo $contents;//printing data of file
fclose($handle);//close file
?>
Output:
This is a Text File
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
Example: fgets()
<?php
$fp = fopen("D:\VNM\Files\file.txt", "r");//open file in read mode echo fgets($fp);
fclose($fp);
?>
Output:
This is a Text File
3. String Manipulations And File Handling Centre for Distance and Online
Education
Web Programming
3. String Manipulations And File Handling Centre for Distance and Online
Education