PHP Programs8to12
PHP Programs8to12
Develop a PHP program to read the content of the file and print the frequency of
occurrence of the word accepted by the user in the file.
<html>
<body>
<form method="post">
<br>
<br>
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
function countWordFrequency($fileName,$searchWord){
if(!file_exists($fileName)){
}
$fileContent=file_get_contents($fileName);
$wordFrequency=substr_count(strtolower($fileContent),strtolower($searchWo
rd));
return $wordFrequency;
}
$fileName=$_POST['fileName'];
$searchWord=$_POST['searchWord'];
$wordFrequency=countWordFrequency($fileName,$searchWord);
if(is_numeric($wordFrequency)){
echo "<p>The word '$searchWord' occurs
<b>$wordFrequency</b> times in the <b>file</b>";
}else{
"<p>$wordFrequency</p>";
?>
</body>
</html>
Program 9.
<?php
$array1 = array('c1' => 'Red', 'c2' => 'Green', 'c3' => 'White', 'c4' => 'Black');
$filteredArray = array();
foreach ($array as $key => $value)
if (in_array($key, $keyNames))
$filteredArray[$key] = $value;
return $filteredArray;
print_r($filteredArray1);
//print_r($filteredArray2);
?>
Program 10.
<html>
<body>
<form method="post">
name="emp_date_of_joining"><br><br>
</form>
<?php
class Employee {
public $emp_name;
public $emp_id;
public $emp_dept;
public $emp_salary;
public $emp_date_of_joining;
$this->emp_name = $name;
$this->emp_id = $id;
$this->emp_dept = $dept;
$this->emp_salary = $salary;
$this->emp_date_of_joining = $date_of_joining;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$emp_id = $_POST["emp_id"];
$emp_dept = $_POST["emp_dept"];
$emp_salary = $_POST["emp_salary"];
$emp_date_of_joining = $_POST["emp_date_of_joining"];
// Create an object of Employee class with submitted data
$employee = new Employee($emp_name, $emp_id, $emp_dept, $emp_salary,
$emp_date_of_joining);
$employee->printDetails();
?>
</body>
</html>
Program 11 a.
<html>
<head>
</head>
<body>
<form method="post">
</form>
<?php
function countAadhaarNumbers($text)
{
$pattern = '/\b\d{4}\s\d{4}\s\d{4}\b/';
return $count;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input_text = $_POST["text"];
$occurrences = countAadhaarNumbers($input_text);
// Output the result
echo "<h2>Result:</h2>";
?>
</body>
</html>
Program11b.
<html>
<head>
<title>Pattern Replacement</title>
</head>
<body>
<h2>Pattern Replacement</h2>
<form method="post">
Input Text:<textarea id="input_text" name="input_text" rows="4"
cols="50"></textarea><br><br>
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input_text = $_POST["input_text"];
$pattern = $_POST["pattern"];
$replacement_text = $_POST["replacement_text"];
echo "<h2>Result:</h2>";
?>
</body>
</html>
Program12.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Display the form data
<html>
<body>
<h1>HTML Form</h1>
<form method="post">
</form>
</body>
</html>