0% found this document useful (0 votes)
7 views5 pages

PHP Practice 6

The document contains PHP code snippets demonstrating various string manipulation functions such as strlen(), strstr(), strpos(), substr(), and str_replace(). It also illustrates how to format dates, trim strings, replace substrings, convert new lines to breaks, and wrap long words. Overall, it serves as a practical guide for handling strings in PHP programming.

Uploaded by

laxmibengeri2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

PHP Practice 6

The document contains PHP code snippets demonstrating various string manipulation functions such as strlen(), strstr(), strpos(), substr(), and str_replace(). It also illustrates how to format dates, trim strings, replace substrings, convert new lines to breaks, and wrap long words. Overall, it serves as a practical guide for handling strings in PHP programming.

Uploaded by

laxmibengeri2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

/*strlen() requires a string and returns an integer representing the number of characters in the

variable you have passed it.*/

<?php

error_reporting(0);

if (strlen($membership) == 4) {

echo "<p>Thank you!</p>";

} else {

echo "<p>Your membership number must have 4 digits</p>";

/*requires two arguments: a source string and the substring you

want to find within it. The function returns false if the substring is absent.*/

$membership = "pAB7";

if (strstr($membership, "MN")) {

echo "<p>Thank you. Don't forget that your membership expires soon!</p>";

} else {

echo "<p>Thank you!</p>";

/*This function tells you both whether a string exists within a larger string

and where it is to be found.*/

$membership = "mz00xyz"; //change position and check

if (strpos($membership, "mz") === 0) {


echo "hello mz";

//returns a portion of a string based on the start index and length of the portion you are looking
for.demands two arguments:a source string and the starting index

$test = "scallywag";

echo substr($test,6)."<br>";

echo substr($test,6,2)."<br>";

//example 2:

$test = "amazon.co.in";

if ($test = substr($test, -3) == ".in") { //what does negative indicate

echo "<p>Shop at Amazon India!</p>";

} else {

echo "<p>Welcome to our shop!</p>";

//Shows the use of sprintf

$store = sprintf("%.3f", 2.334454);

echo "You have $store value to use later";

//string replace

<?php

echo str_replace("Universe", "World", "Hello Universe of Universe!"),"<br>";

echo str_replace("Java", "PHP", "Hello Class of Java!"),"<br>";

//reverse of a string

echo strrev("Hello world!"),"<br>";


//Counting words in a string

echo str_word_count("Hello world!"),"<br>";

$str="I Have a Story TO TELL";

$str=strtolower($str);

echo $str."<br>";

$str="I Have a Story TO TELL";

$str=strtoupper($str);

echo $str."<br>";

//Argument swapping

<?php

$dates = array(

array('mon'=> 12, 'mday'=>25, 'year'=>2002),

array('mon'=> 5, 'mday'=>23, 'year'=>2003),

array('mon'=> 10, 'mday'=>29, 'year'=>2002)

);

$format = include("local_format.php");

foreach($dates as $date) {

printf("$format", $date['mon'], $date['mday'], $date['year']);

//file local_format.php

return "%02d/%02d/%d<br>";

//return "%2\$02d/%1\$02d/%3\$d<br>";(remove comment lines and execute)

<?php
$text = "\t\tToday is the quiz event";

echo "<pre>$text</pre>";

$text = trim($text);

echo "<pre>$text</pre>";

//trimming only right part

$text = "\t\tlots of room to breathe ";

echo "<pre>$text</pre>";

// prints " lots of room to breathe ";

$text = rtrim($text);

echo $text;

//trimming only left part

$text = "\t\tlots of room to breathe ";

echo "<pre>$text</pre>";

// prints " lots of room to breathe ";

$text = ltrim($text);

echo "<pre>$text</pre>";

/*substring replacement example.The function requires three

arguments: the string you are transforming, the text you want to add to it, and

the starting index. It also accepts an optional length argument.*/

$membership = "mz02xyz";

$membership = substr_replace($membership, "03", 2, 2);


echo "New membership number: $membership";

//converting new line to breaks

$string = "one line\n";

$string .= "another line\n";

$string .= "a third line to test\n";

echo nl2br($string);

/*wordwrap() function wraps a string into new lines when it reaches a specific length,syntax:
wordwrap(string,width,break,cut)*/

/*parameter string is mandatory,others are optional,default width is 75,default break is "\


n",specifies whether words longer than the specified

width should be wrapped*/

$str="An example of a long word

is: Supercalifragulistic";

echo wordwrap($str,15,"<br>\n");

?>

You might also like