/    Sign up×
Community /Pin to ProfileBookmark

For each and search string?

Hi,

I want to search a string and look for several words from an array and use a for each loop to go through the array. But I don’t know how to combine the code because I want the following code only to be activatet if there is no hit in the array, like !=.

Preciate some help. Thanks!

[COLOR=”Red”]$filter = array(“name1″,”name2″,”name3”);

$search = strstr($text, ??

for each ($filter as ??[/COLOR]

Following code to run if there is no match

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@dbkJan 14.2011 — Hi pkng

Not sure if I understod your question correct, and I have never tried the strstr function, but what about moving the loop..

[code=php]$filter = array("name1","name2","name3");

foreach ($filter as $key => $name_to_check)
{
$search = strstr($text, $name_to_check);
}[/code]


You could also make $search an array to avoid the value to be overwritten!

Hope this is a help!
Copy linkTweet thisAlerts:
@thraddashJan 14.2011 — LOL, I was doing one too...

[code=php]<?php

$filter = array('name1', 'name2', 'name3');
$text = 'Is this your name2 or name1?';

$matches = array();
foreach ($filter as $item) {
if (strpos($text, $item) !== false) {
$matches[] = $item;
}
}

echo 'Searching: <b>' . $text . '</b><br /><br />';
if ($matches) {
echo count($matches) . ' MATCHES FOUND';
echo '<pre>' . print_r($matches, true) . '</pre>';
} else {
echo 'NO MATCHES FOUND';
}[/code]


$matches will return all the words from the array that are in $text.
Copy linkTweet thisAlerts:
@pkngauthorJan 14.2011 — Thanks for the replies!

It's perhaps a little bit hard to explain what I mean. I would like to do something like this....

if [COLOR="Red"]![/COLOR]($name1 == $not not in the string) {

Some code to be activated if there is no match

}

Or in plain english, do this if there is not any name from the array in the text... ?
Copy linkTweet thisAlerts:
@thraddashJan 14.2011 — How about this then...

[code=php]<?php

$filter = array('name1', 'name2', 'name3');
$text = 'Is this your?';

for ($i = 0; $i < count($filter); $i++) {
if (strpos($text, $filter[$i]) !== false) {break;}
}
if ($i >= count($filter)) {
echo 'Some code to be activated if there is no match';
}[/code]


The echo statement only happens if nothing in $filter matches $text.
Copy linkTweet thisAlerts:
@pkngauthorJan 14.2011 — Nice! Thanks!

?
×

Success!

Help @pkng spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...