0% found this document useful (0 votes)
5 views8 pages

PHP Programs8to12

lab manual

Uploaded by

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

PHP Programs8to12

lab manual

Uploaded by

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

8.

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>

<h2>Word Frequency Counter</h2>

<form method="post">

Enter file name:<input type="test" name="fileName" required>

<br>

Enter word to search:<input type="text" name="searchWord" required>

<br>

<input type="Submit" value="Search">

</form>

<?php

if($_SERVER["REQUEST_METHOD"]=="POST"){

function countWordFrequency($fileName,$searchWord){

if(!file_exists($fileName)){

return "Error:File not found";

}
$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');

$array2 = array('12', 'c');

function filterArrayByKey($array, $keyNames)

$filteredArray = array();
foreach ($array as $key => $value)

if (in_array($key, $keyNames))

$filteredArray[$key] = $value;

return $filteredArray;

$filterKeyNames = array('c1', 'c3');


$filteredArray1 = filterArrayByKey($array1, $filterKeyNames);

$filteredArray2 = filterArrayByKey($array2, $filterKeyNames);

echo "Filtered Array 1: ";

print_r($filteredArray1);

//echo "Filtered Array 2: ";

//print_r($filteredArray2);
?>

Program 10.

<html>

<body>

<h2>Enter Employee Details</h2>

<form method="post">

Employee Name:<input type="text" id="emp_name" name="emp_name"><br>

Employee ID:<input type="text" id="emp_id" name="emp_id"><br>

Employee Department: <input type="text" id="emp_dept" name="emp_dept"><br>

Employee Salary: <input type="text" id="emp_salary" name="emp_salary"><br>

Employee Date of Joining: <input type="text" id="emp_date_of_joining"

name="emp_date_of_joining"><br><br>

<input type="submit" value="Submit">

</form>
<?php

// Define the Employee class

class Employee {

public $emp_name;

public $emp_id;
public $emp_dept;
public $emp_salary;

public $emp_date_of_joining;

// Constructor to initialize object properties

public function __construct($name, $id, $dept, $salary, $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;

// Method to print employee details

public function printDetails() {

echo "<h2>Employee Details</h2>";

echo "Employee Name: " . $this->emp_name . "<br>";

echo "Employee ID: " . $this->emp_id . "<br>";

echo "Employee Department: " . $this->emp_dept . "<br>";

echo "Employee Salary: " . $this->emp_salary . "<br>";

echo "Employee Date of Joining: " . $this->emp_date_of_joining . "<br>";

// Check if form is submitted

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Retrieve employee data from POST


$emp_name = $_POST["emp_name"];

$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);

// Print employee details

$employee->printDetails();

?>

</body>

</html>

Program 11 a.

<html>

<head>

<title>Aadhaar Number Occurrence Counter</title>

</head>

<body>

<h2>Enter Text to Count Aadhaar Numbers</h2>

<form method="post">

<textarea name="text" rows="4" cols="50"></textarea><br><br>

<input type="submit" name="submit" value="Count Aadhaar Numbers">

</form>

<?php

function countAadhaarNumbers($text)
{

// Regular expression to match Aadhaar numbers

$pattern = '/\b\d{4}\s\d{4}\s\d{4}\b/';

// Match all occurrences of Aadhaar numbers in the text

preg_match_all($pattern, $text, $matches);


// Count the number of matches
$count = count($matches[0]);

return $count;

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Retrieve the text input from the form

$input_text = $_POST["text"];

// Call the function to count Aadhaar numbers

$occurrences = countAadhaarNumbers($input_text);
// Output the result

echo "<h2>Result:</h2>";

echo "Number of Aadhaar numbers found: $occurrences";

?>

</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>

Pattern to Replace:<input type="text" id="pattern" name="pattern"><br><br>

Replacement Text:<input type="text" id="replacement_text"


name="replacement_text"><br><br>

<input type="submit" name="submit" value="Replace Pattern">


</form>
<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Retrieve input values from the form

$input_text = $_POST["input_text"];

$pattern = $_POST["pattern"];

$replacement_text = $_POST["replacement_text"];

// Perform the replacement

$result_text = preg_replace("/$pattern/", $replacement_text, $input_text);


// Output the result

echo "<h2>Result:</h2>";

echo "Original Text: <br>" . nl2br($input_text) . "<br><br>";

echo "Replaced Text: <br>" . nl2br($result_text);

?>

</body>

</html>
Program12.

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Retrieve form data

$name = $_POST["name"];

$email = $_POST["email"];

$message = $_POST["message"];
// Display the form data

echo "Form Data:";

echo "<strong>Name:</strong> $name";

echo "<strong>Email:</strong> $email";

echo "<strong>Message:</strong> $message";


}
?>

<html>

<body>

<h1>HTML Form</h1>

<form method="post">

Name:<input type="text" id="name" name="name"><br>

Email:<input type="email" id="email" name="email"><br>

Message:<textarea id="message" name="message"></textarea><br>


<input type="submit" value="Submit">

</form>

</body>

</html>

You might also like