0% found this document useful (0 votes)
29 views6 pages

Unit IV Rest

The document discusses several PHP string functions: - strpos() searches for a substring within a string and returns its position. If not found, it returns FALSE. - str_replace() replaces some characters with others in a string following certain rules, such as using an empty string if replace is shorter than find. - Arrays can store multiple values in a variable and are useful for lists. PHP has indexed, associative, and multidimensional arrays. count() returns the length of an array.
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)
29 views6 pages

Unit IV Rest

The document discusses several PHP string functions: - strpos() searches for a substring within a string and returns its position. If not found, it returns FALSE. - str_replace() replaces some characters with others in a string following certain rules, such as using an empty string if replace is shorter than find. - Arrays can store multiple values in a variable and are useful for lists. PHP has indexed, associative, and multidimensional arrays. count() returns the length of an array.
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/ 6

Searching Strings

strpos() - Search For a Text Within a String


The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of
the first match. If no match is found, it will return FALSE.

Example

Search for the text "world" in the string "Hello world!":

replacing and formatting strings


PHP str_replace() Function
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

Syntax
str_replace(find,replace,string,count)

Example

Replace the characters "world" in the string "Hello world!" with "Peter":

PHP Arrays
 An array stores multiple values in one single variable
 An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The solution is to create an array!

An array can hold many values under a single name, and you can access the values by referring to an index number.

Create an Array in PHP


In PHP, the array() function is used to create an array:

array();

Get The Length of an Array - The count() Function


The count() function is used to return the length (the number of elements) of an array

In PHP, there are three types of arrays:

 Indexed arrays - Arrays with a numeric index


 Associative arrays - Arrays with named keys
 Multidimensional arrays - Arrays containing one or more arrays
Example

You might also like