Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

PHP

(Web Development Technology)


Searching string
str_contains()
str_contains — Determine if a string contains a given substring
Description
str_contains ( string $haystack , string $needle ) : bool
Performs a case-sensitive check indicating if needle is contained in haystack.

Parameters
haystack
The string to search in.

needle
The substring to search for in the haystack.
Return Values
Returns true if needle is in haystack, false otherwise.
<?php
$string = 'The lazy fox jumped over the fence';

if (str_contains($string, 'lazy')) {
    echo "The string 'lazy' was found in the string\n";
}

if (str_contains($string, 'Lazy')) {
    echo 'The string "Lazy" was found in the string';
} else {
    echo '"Lazy" was not found because the case does not match';
}

?>
Replacing String

str_replace()
The str_replace() function replaces some characters with some other characters in a string.
This function works by the following rules:
•If the string to be searched is an array, it returns an array
•If the string to be searched is an array, find and replace is performed with every array
element
•If both find and replace are arrays, and replace has fewer elements than find, an empty
string will be used as replace
•If find is an array and replace is a string, the replace string will be used for every find
value
Note: 
This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive
search.
Syntax

str_replace(find,replace,string,count)

Parameter Values

Parameter Description
find Required. Specifies the value to find
replace Required. Specifies the value to replace the value in find
string Required. Specifies the string to be searched
count Optional. A variable that counts the number of replacements
Example

<?php
echo str_replace("world","Peter","Hello world!");
?>

Example

Using str_replace() with an array and a count variable:

<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";
?>
Formatting String

The sprintf() function is used to output a formatted string.

Syntax

sprintf(format, arg1, arg2, arg++)

Parameters

format − Specifies the string and how to format the variables in it.

The following are the possible format values −


•%% - Returns a percent sign
•%b - Binary number
•%c - The character according to the ASCII value
•%d - Signed decimal number (negative, zero or positive)
•%e - Scientific notation using a lowercase (e.g. 1.2e+2)
•%E - Scientific notation using a uppercase (e.g. 1.2E+2)
•%u - Unsigned decimal number (equal to or greater than zero)
•%f - Floating-point number (local settings aware)
•%F - Floating-point number (not local settings aware)
•%g - shorter of %e and %f
•%G - shorter of %E and %f
•%o - Octal number
•%s - String
•%x - Hexadecimal number (lowercase letters)
•%X - Hexadecimal number (uppercase letters)

•argument1 − The argument to be inserted at the first %-sign in the format string.
•argument2 − The argument to be inserted at the second %-sign in the format
string.

Return
The sprintf() function returns a formatted string.
Example

<?php
$val = 299;
$txt = sprintf("%f",$val);
echo $txt;
?>

Example
Let us see another example −

<?php
$val = 768776;
$char = 95;
echo sprintf("%%b = %b",$val)."<br>";
echo sprintf("%%c = %c",$char);
?>
PHP Superglobal - $_REQUEST

PHP $_REQUEST is a PHP super global variable which is used to collect


data after submitting an HTML form.

The example below shows a form with an input field and a submit button.
When a user submits the data by clicking on "Submit", the form data is sent
to the file specified in the action attribute of the <form> tag. In this example,
we point to this file itself for processing form data. If you wish to use
another PHP file to process form data, replace that with the filename of your
choice. Then, we can use the super global variable $_REQUEST to collect
the value of the input field:
Example
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = $_REQUEST['fname'];
  if (empty($name)) {
    echo "Name is empty";
  } else {
    echo $name;
  }
}
?>
</body>
</html>

You might also like