Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Search for PHP array element containing string?
Let’s say we have the following array
$subject= array('JavaScript','Java','PHP language','Python Language');
From the above array, we need to fetch values with the following text
$valueToSearch= 'Language';
For such match, use preg_match() in PHP.
Example
The PHP code is as follows
<!DOCTYPE html>
<html>
<body>
<?php
$subject= array('JavaScript','Java','PHP language','Python Language');
$valueToSearch= 'Language';
$result = array();
foreach($subject as $t=>$value) {
if(preg_match("/\b$valueToSearch\b/i", $value)){
$result[$t] = $value;
}
}
print_r($result);
?>
</body>
</html>
Output
This will produce the following output
Array ( [2] => PHP language [3] => Python Language )
Advertisements
