JNTU
JNTU B.Tech III Year WEB TECHNOLOGIES Previous Question Paper (September - 2021 - R18)
Answer any five questions
All questions carry equal marks
Main
Discuss about various functions used in PHP with suitable coding examples.
Answer
JNTU
WEB TECHNOLOGIES
B.Tech, CSE, R18, III-I
Exam Month-Year: May-2024, Max Marks: 75, Time: 3 Hours
Instructions: Answer all questions. Each question carries equal marks.
Discuss about various functions used in PHP with suitable coding examples.
Marks: 8
- PHP offers a vast collection of built-in functions that simplify common programming tasks. These functions can be broadly categorized based on their purpose.
- String Functions: Manipulate string data. Examples include:
strlen(): Returns the length of a string.
<?php $text = "Hello World"; echo strlen($text); // Output: 11 ?> str_replace(): Replaces all occurrences of a substring with another substring.
<?php
$original = "The quick brown fox";
$new = str_replace("brown", "red", $original);
echo $new; // Output: The quick red fox
?>substr(): Extracts a part of a string.<?php
$string = "Hello";
echo substr($string, 1, 3); // Output: ell
?>count(): Returns the number of elements in an array.
<?php
$fruits = array("apple", "banana", "cherry");
echo count($fruits); // Output: 3
?>implode(): Joins array elements into a string.<?php
$array = array("Hello", "World");
$text = implode(" ", $array);
echo $text; // Output: Hello World
?>sort(): Sorts an array in ascending order.<?php
$numbers = array(4, 6, 2, 22);
sort($numbers);
print_r($numbers); // Output: Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 22 )
?>abs(): Returns the absolute value of a number.
<?php
echo abs(-4.7); // Output: 4.7
?>sqrt(): Returns the square root of a number.<?php
echo sqrt(9);
?>round(): Rounds a floating-point number.<?php
echo round(3.14159, 2); // Output: 3.14
?>date(): Formats a local date and time.
<?php
echo date("Y-m-d H:i:s"); // Output: e.g., 2024-05-15 10:30:00
?>time(): Returns the current Unix timestamp.<?php
echo time(); // Output: current Unix timestamp
?>fopen(): Opens a file or URL.fclose(): Closes an open file pointer.fread(): Reads from an open file.
<?php
$file = fopen("test.txt", "r");
if ($file) {
$content = fread($file, filesize("test.txt"));
echo $content;
fclose($file);
}
?>mysqli_connect(): Establishes a new connection to a MySQL database.mysqli_query(): Performs a query on the database.mysqli_fetch_assoc(): Fetches a result row as an associative array.
<?php
$conn = mysqli_connect("localhost", "username", "password", "database");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, name FROM users";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>Write a PHP script for searching for a web page.
Answer
JNTU
WEB TECHNOLOGIES
B.Tech, CSE, R18, III-I
Exam Month-Year: May-2024, Max Marks: 75, Time: 3 Hours
Instructions: Answer all questions. Each question carries equal marks.
Write a PHP script for searching for a web page.
Marks: 7
Searching for a web page typically involves fetching the content of a given URL and then performing a search within that content. PHP can achieve this using functions like file_get_contents() or cURL to retrieve the page content, and then string manipulation functions to find the desired keyword.
Here's a PHP script that demonstrates how to search for a keyword on a specific web page:
<?php
// Function to search for a keyword on a given URL
function searchWebPage($url, $keyword) {
// Use file_get_contents to fetch the HTML content of the URL
// @ suppresses warnings if the URL is not accessible
$htmlContent = @file_get_contents($url);
if ($htmlContent === FALSE) {
return "<p style='color:red'>Error: Could not retrieve content from URL: " . htmlspecialchars($url) . "</p>";
}
// Use stripos to find the position of the keyword (case-insensitive)
// stripos returns the position of the first occurrence of a substring in a string, case-insensitive.
// It returns FALSE if the keyword is not found.
$position = stripos($htmlContent, $keyword);
if ($position !== FALSE) {
// Keyword found
return "<p style='color:green'>Keyword '<b>" . htmlspecialchars($keyword) . "</b>' found on page '<b>" . htmlspecialchars($url) . "</b>' at position " . ($position + 1) . ".</p>";
} else {
// Keyword not found
return "<p style='color:orange'>Keyword '<b>" . htmlspecialchars($keyword) . "</b>' not found on page '<b>" . htmlspecialchars($url) . "</b>'.</p>";
}
}
// --- Example Usage ---
// Define the URL to search and the keyword
$targetUrl = "https://www.example.com"; // Replace with the actual URL
$searchKeyword = "Example"; // Replace with the keyword you want to search for
// Call the function and display the result
echo searchWebPage($targetUrl, $searchKeyword);
// Another example with a different keyword
$searchKeyword2 = "NonExistentKeyword";
echo searchWebPage($targetUrl, $searchKeyword2);
// Example with a potentially problematic URL (for testing error handling)
$badUrl = "http://thisisnotavalidurl12345.com";
$keywordForBadUrl = "test";
echo searchWebPage($badUrl, $keywordForBadUrl);
?>
Explanation:
searchWebPage($url, $keyword)function: This function encapsulates the logic for searching. It takes the URL and the keyword as parameters.@file_get_contents($url): This is the core function used to fetch the entire content of the web page into a string. The '@' symbol is used to suppress any potential warnings or errors if the URL is invalid or inaccessible, allowing us to handle errors gracefully.- Error Handling: The script checks if
file_get_contents()returnedFALSE, which indicates a failure to retrieve the page content. If an error occurs, an informative message is returned. stripos($htmlContent, $keyword): This function searches for the first occurrence of the$keywordwithin the$htmlContentstring. It is case-insensitive, meaning it will find "Example", "example", or "EXAMPLE". It returns the starting position (index) of the keyword if found, orFALSEif not found.- Result Output: Based on whether the keyword was found (
$position !== FALSE), the script returns a user-friendly message indicating success or failure, along with the URL and the keyword. htmlspecialchars(): This function is used to prevent cross-site scripting (XSS) vulnerabilities by converting special characters into HTML entities. It's good practice to use this when displaying user-provided or external data.
Note: This script performs a simple text search. For more complex web scraping or searching (e.g., searching within specific HTML tags, handling JavaScript-rendered content, or dealing with large amounts of data), more advanced techniques like using cURL with options for headers and timeouts, or employing dedicated scraping libraries, might be necessary.
Summarize about various types of PHP interpreters.
Answer
Sign in to view the complete answer paper.
Write a PHP script to add a user to Login table in MySql database.
Answer
Sign in to view the complete answer paper.
What is XML Schema? List the advantages of it over DTD.
Answer
Sign in to view the complete answer paper.
Describe XML schema for designing a web page for product display.
Answer
Sign in to view the complete answer paper.
What is XML? List characteristic features of XML.
Answer
Sign in to view the complete answer paper.
What are XML namespaces? Describe the XML namespace for any webpage.
Answer
Sign in to view the complete answer paper.
Explain the life cycle of servlet? Write a simple servlet program to read two parameters from form data.
Answer
Sign in to view the complete answer paper.
Distinguish Generic servlet from HttpServlet.
Answer
Sign in to view the complete answer paper.
Write a servlet program to retrieve data from the Employee table which is existing in MySql database.
Answer
Sign in to view the complete answer paper.
Explain the advantages of servlets over CGI scripts.
Answer
Sign in to view the complete answer paper.
What are the implicit objects available in JSP? List the advantages of JSP over Servlets?
Answer
Sign in to view the complete answer paper.
Explain about page directive with an example JSP code.
Answer
Sign in to view the complete answer paper.
Explain about Document Object Model with suitable example.
Answer
Sign in to view the complete answer paper.
Write a Java Script function to check whether given number is prime or not.
Answer
Sign in to view the complete answer paper.