2

Right now I am working on a search function for my json decoded results

The code that I got looks like this:

<?php
foreach($soeg_decoded as $key => $val){
    $value = $val["Value"];
    $seotitle = $val["SEOTitle"];
    $text = $val["Text"];

echo '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
}
?>

I want it to only show the results, which contains a specific word, which are stored in a variable named $searchText.

Is there any functions or commands, which can seperate all the results, and only show the results which contains the word stored in $searchText.

Something like this:

if($value OR $seotitle OR $text contains $searchText){
    echo '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
}

Thanks in advance.

4 Answers 4

3

You can use substr_count()

<?php
foreach($soeg_decoded as $key => $val){
    $value = $val["Value"];
    $seotitle = $val["SEOTitle"];
    $text = $val["Text"];

  if(substr_count($value, $searchText) || substr_count($seotitle, $searchText) ||  substr_count($text, $searchText)){
      echo '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
  }

}
?>

Read more at:

http://php.net/manual/en/function.substr-count.php

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!!! I added a if before, so it also could contain an empty searchText variable. This helped me alot!!
strpos() might be a cheaper operation since it looks for the first occurrence. You don't need seem to be needing the count and substr_count() will be reading the whole string to find the occurrences which is more expensive.
3

You can use strpos string function to check if the $searchText (Needle) is part of $value/$seotitle/$text (Haystack).

if ((strpos($value,$searchText) !== false) or 
(strpos($seotitle,$searchText) !== false) or 
(strpos($text,$searchText) !== false)) 
{
    echo '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
}

1 Comment

It looks like there was a flood of similar answers, while I was typing this!
3

This would probably be the simplest way to do it:

$row = '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
if (empty($searchText) || strpos($searchText, $row) !== false) {
    echo $row;
}

This could have false matches if $searchText contains <td> or something, if you are concerned about that you could do:

if (count(array_filter([$value, $seotitle, $text], function($v) use ($searchText) {
    return empty($searchText) || strpos($searchText, $v) !== false;
}))) {
    echo '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
}

2 Comments

Thanks, that is kind of what i was looking for :D But is there a way to make it show the row, even if the searchText is empty? Because it is not needed to write a searchText :)
See edit, it will show if searchText is empty or if not empty, if it matches searchText
3

You can do it by using strpos():-

if(strpos($value,$searchText) !== FALSE || strpos($seotitle,$searchText) !== FALSE || strpos($text,$searchText) !== FALSE){
    echo '<tr><td>' . $value . '</td><td>' . $seotitle . '</td><td>' . $text . '</td></tr>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.