Home » » Search array values in string in php

Search array values in string in php

Written By Unknown on Kamis, 22 Maret 2012 | 09.04

If we have to search an array values in string two possible situations arise.

1. The string contains all array values.
2. The string contains at least one array value.

Here are two little functions that searches for an array value in string.

At least one array value exists in string

  function array_in_string($string, $array) {
foreach ($array as $value) {
$status=stristr($string, $value);
if ($status) {
return true;
}
}
return false;
}


Example of above function

$text=array('text', 'search');
$string='This is the search string';

$match=array_in_string($string,$text);

if($match){
echo 'Found';
}else{
echo 'Not found';
}

All array values exist in string

  function array_in_string($string, $array) {

foreach ($array as $value) {
$status=stristr($string, $value);
if (!$status) {
return false;
}
}
return true;
}


Example of above function

$text=array('text', 'search');
$string='This is the search string';

$match=array_in_string($string,$text);

if($match){
echo 'Found';
}else{
echo 'Not found';
}
Share this article :

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger