Ass 3
Ass 3
Ass 3
Code 1:
<html>
<head>
<title>String Operations</title>
</head>
<body>
<form method="GET" action="string.php">
<label for="string">Enter a string:</label>
<input type="text" id="string" name="string" required><br><br>
Code 2:
<?php
if (isset($_GET['string']) && isset($_GET['operation'])) {
$string = $_GET['string'];
$operation = $_GET['operation'];
$result = '';
switch ($operation) {
case 'length':
$result = strlen($string);
echo "<p>The length of the string is: $result</p>";
break;
case 'uppercase':
$result = strtoupper($string);
echo "<p>The string in uppercase is: $result</p>";
break;
case 'lowercase':
$result = strtolower($string);
echo "<p>The string in lowercase is: $result</p>";
break;
case 'ucfirst':
$result = ucfirst($string);
echo "<p>The string with the first character uppercase is: $result</p>";
break;
case 'ucwords':
$result = ucwords($string);
echo "<p>The string with the first character of all words uppercase is: $result</p>";
break;
default:
echo "<p>Invalid operation selected.</p>";
break;
}
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="1.php"></a>
</body>
</html>
Output:
a)
b)
c)
d)
e)
2. Write a PHP script to check whether a string contains a specific string or not?
Sample string : 'The quick brown fox jumps over the lazy dog.'
Check whether the said string contains the string 'jumps'.
Code 1:
<html>
<head>
<title>String Contains Check</title>
</head>
<body>
<form method="GET" action="two.php">
<label for="main_string">Enter the main string:</label><br>
<textarea id="main_string" name="main_string" rows="4" cols="50" placeholder="The quick brown
fox jumps over the lazy dog." required></textarea><br><br>
<label for="search_string">Enter the string to search for:</label>
<input type="text" id="search_string" name="search_string" value="jumps" required><br><br>
<input type="submit" value="Check">
</form>
</body>
</html>
Code 2:
<?php
if (isset($_GET['main_string']) && isset($_GET['search_string'])) {
$main_string = $_GET['main_string'];
$search_string = $_GET['search_string'];
if (strpos($main_string, $search_string) !== false) {
echo "<p>The string '$main_string' contains '$search_string'.</p>";
} else {
echo "<p>The string '$main_string' does not contain '$search_string'.</p>";
}
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="2.php"></a>
</body>
</html>
Output:
3. Write a PHP program to reverse a given string.
Code 1:
<html>
<head>
<title>String Reversal</title>
</head>
<body>
<form method="GET" action="three.php">
<label for="string">Enter a string to reverse:</label>
<input type="text" id="string" name="string" required><br><br>
<input type="submit" value="Reverse String">
</form>
</body>
</html>
Code 2:
<?php
if (isset($_GET['string'])) {
$string = $_GET['string'];
$reversed_string = strrev($string);
echo "<p>The reversed string is: $reversed_string</p>";
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="3.php"></a>
</body>
</html>
Output:
4. Write a PHP script to extract the file name from the following string.
Sample String : 'www.example.com/public_html/index.php'
Expected Output : 'index.php' following content under this heading:
Code 1:
<html>
<head>
<title>Extract File Name</title>
</head>
<body>
<form method="GET" action="four.php">
<label for="path">Enter the full file path:</label>
<input type="text" id="path" name="path"
placeholder="file:///C:/xampp/htdocs/IU65/assignment/ass-2/ass-2.pdf" required><br><br>
<input type="submit" value="Extract File Name">
</form>
</body>
</html>
Code 2:
<?php
if (isset($_GET['path'])) {
$path = $_GET['path'];
$file_name = basename($path);
echo "<p>The extracted file name is: $file_name</p>";
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="4.php"></a>
</body>
</html>
Output:
5. Write a PHP script to extract the user name from the following email ID.
Sample String : '[email protected]'
Expected Output : 'rayy'
Code 1:
<html>
<head>
<title>Extract Username from Email</title>
</head>
<body>
<form method="GET" action="five.php">
<label for="email">Enter an email ID:</label>
<input type="text" id="email" name="email" placeholder="[email protected]" required><br><br>
<input type="submit" value="Extract Username">
</form>
</body>
</html>
Code 2:
<?php
if (isset($_GET['email'])) {
$email = $_GET['email'];
$username = strstr($email, '@', true);
echo "<p>The extracted username is: $username</p>";
}
?>
<html>
<head>
<title>Result</title>
</head>
<body>
<a href="5.php"></a>
</body>
</html>
Output:
6. Write a PHP script to replace the first 'the' of the following string with 'That'.
Sample date : 'the quick brown fox jumps over the lazy dog.'
Expected Result : That quick brown fox jumps over the lazy dog..
Code:
<?php
$string = 'the quick brown fox jumps over the lazy dog.';
$search = 'the';
$replace = 'That';
$result = preg_replace('/\b' . $search . '\b/', $replace, $string,1);
echo $result;
?>
Output:
7. Write a PHP script to print the next character of a specific character.
Sample character : 'a'
Expected Output : 'b'
Sample character : 'z'
Expected Output : 'a'
Code:
<?php
function getNextCharacter($char) {
if ($char === 'z') {
return 'a';
} elseif ($char === 'Z') {
return 'A';
} else {
$nextCharAscii = ord($char) + 1;
return chr($nextCharAscii);
}
}
$value1 = 'a';
$value2 = 'z';
$value3 = 'A';
$value4 = 'Z';
echo "Next character after '$value1' is '" . getNextCharacter($value1) . "'\n";
echo "Next character after '$value2' is '" . getNextCharacter($value2) . "'\n";
echo "Next character after '$value3' is '" . getNextCharacter($value3) . "'\n";
echo "Next character after '$value4' is '" . getNextCharacter($value4) . "'\n";
?>
Output:
8. Write a PHP script to insert a string at the specified position in a given string.
Original String : 'The brown fox'
Insert 'quick' between 'The' and 'brown'.
Expected Output : 'The quick brown fox'
Code:
<?php
function insertString($originalString, $stringToInsert, $position) {
Code:
<?php
function choosePartOfString($choice, $string, $startIndex, $length = null) {
switch($choice) {
case 'a':
return substr($string, $startIndex);
case 'b':
return substr($string, $startIndex, $length);
case 'c':
return substr($string, -$startIndex);
case 'd':
return substr($string, -$startIndex, $length);
default:
return "Invalid choice!";
}
}
$string = "The quick brown fox jumps over the lazy dog";
$choice = 'a';
$startIndex = 4;
$length = 5;
$result = choosePartOfString($choice, $string, $startIndex, $length);
echo $result;
?>
Output:
Output:
13. Write a PHP script to split a string as array elements based on delimiter.
Code:
<?php
$string = "Apple,Banana,Cherry,Date,Elderberry";
$delimiter = ",";
$array = explode($delimiter, $string);
echo "The array elements are:\n";
foreach ($array as $element) {
echo $element . "\n";
}
?>
Output:
14. Write a PHP program to combine the array elements into a string with given delimiter
Code:
<?php
$array = array("Apple", "Banana", "Cherry", "Date", "Elderberry");
$delimiter = ", ";
$combinedString = implode($delimiter, $array);
echo "The array elements combined into a string is:\n";
echo $combinedString;
?>
Output:
15. Write a PHP program to remove the duplicate values from an array.
Code:
<?php
$array = array("Apple", "Banana", "Cherry", "Apple", "Date", "Banana", "Elderberry");
$uniqueArray = array_unique($array);
echo "The array with duplicate values removed is:\n";
foreach ($uniqueArray as $element) {
echo $element . "\n";
}
?>
Output: