- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 )
- Related Articles
- How to search for an element in JavaScript array?
- Search for partial value match in an Array in PHP
- C# Program to search for a string in an array of strings
- C++ Program to Search for an Element in a Binary Search Tree
- C program to search an array element using Pointers.
- Using XPATH to search text containing  
- How to search for exact string in MySQL?
- Quickly search for a string in MySQL database?
- How to search for a string in JavaScript?
- Repeatedly Search for an Element by Doubling it After Every Successful Search Using C++
- Removing Array Element and Re-Indexing in PHP
- How to convert array to string PHP?
- Search text containing a new line in MySQL?
- Java Program to Recursively Linearly Search an Element in an Array
- C++ program to search an element in a sorted rotated array

Advertisements