0

I currently have the code all done to filter by one keyword, but I'm wanting to filter by 100+ keywords. I'm not experienced enough in PHP, so I've tried and failed.

Here is my current code but I'm wanting to modify it to use multiple keywords.

function postResolve ($key) {
    return isset($_POST[$key]) ? $_POST[$key] : null;
}

$formData = [
    'keyword' => postResolve('keyword'),
    'text'    => postResolve('text')
];

if (is_null($formData['keyword']) || is_null($formData['text'])) {
    return 'Nothing to do... Missing text or keyword.';
}

$text = explode(PHP_EOL, $formData['text']);

$storage = [];

foreach ($text as $sentence) {
    if (strpos($sentence, $formData['keyword']) !== false) {
        $storage[] = trim($sentence);
    }
}

echo json_encode($storage);

Would appreciate if someone could give me a hand on this change :)

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Dzje
  • 65
  • 9
  • if you're using PHP 7, `isset($_POST[$key]) ? $_POST[$key] : null` equals `$_POST[$key] ?? null` – Xorifelse Nov 23 '16 at 22:23
  • I'm not 100% sure PHP is by far my most hated language, I had a friend do this for me. I just modified it to work with bootstrap. – Dzje Nov 23 '16 at 22:25
  • You can use in_array and place the 100+ keywords in your haystack. – Daerik Nov 23 '16 at 22:25
  • How are you getting the multiple keywords? Are they in an array? – Don't Panic Nov 23 '16 at 22:26
  • @Dzje I am not sure where that came from, PHP is used by at least 50% of the webservers and used by a lot of popular websites. – Xorifelse Nov 23 '16 at 22:26
  • Okay so this is just the functionality, i have an index.php that has 1 textbox to enter the keywords in, another multi line text box which will be full with stuff that im wanting to fillter and then there is a button that obviously filters the keywords and removes the rest. So I'll be entering the keywords manually in the text box like example "keyword1, keyword2, keyword3, keyword4" and so on. – Dzje Nov 23 '16 at 22:34

1 Answers1

1

I would suggest using stripos() instead of strpos(). Unlike the strpos(), stripos() is case-insensitive. Treat your keywords as an array. Let's assume they are comma-delimited:

<?php
    function postResolve ($key) {
        return isset($_POST[$key]) ? $_POST[$key] : NULL;
    }

    function checkKeywords($sentence, $keywords) {
        foreach($keywords as $keyword) {
            if(stripos($sentence, $keyword) !== FALSE) { return TRUE; }
        }
        return FALSE;
    }

    if(isset($_POST) && !empty($_POST)) {

        $formData = array(
            'keywords' => postResolve('keywords'),
            'text'    => postResolve('text')
        );

        if(is_null($formData['keywords']) || is_null($formData['text'])) {
            echo 'Nothing to do... Missing text or keyword.';
        }

        $text = explode(PHP_EOL, $formData['text']);
        $keywords = explode(',', $formData['keywords']);

        $storage = array();

        foreach($text as $sentence) {
            if(checkKeywords($sentence, $keywords)) {
                $storage[] = trim($sentence);
            }
        }

        echo json_encode($storage);
        exit();
    }
?>

<form>
    <textarea name="keywords"></textarea>
    <textarea name="text"></textarea>
    <input type="submit" value="Submit">
</form>
<div></div>

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
    $('form').on('submit', function(event) {
        event.preventDefault();
        $('div').load(location.href, $('form').serializeArray());
    });
</script>
Daerik
  • 4,167
  • 2
  • 20
  • 33
  • So lets say for example I'm wanting to filter just two keywords "07883, 07886" the two keywords a separated by the comma and that would work? – Dzje Nov 23 '16 at 22:44
  • 07883,07886 would work unless you explode using ", " as your delimiter. You could always explode on the comma and use an array_map to trim the keywords in case there is a space after the comma. Or just explode on PHP_EOL and have the keywords placed each on a new line like you are doing your text. – Daerik Nov 23 '16 at 22:45
  • yeah that didn't filter, it just resulted in the filtered box returning blank :( – Dzje Nov 23 '16 at 22:53
  • @Dzje paste that entire script into a blank document and it will work. – Daerik Nov 23 '16 at 23:05
  • this is the code for my index.php http://pastebin.com/raw/yWd8ptKc is there any chance to get it working with this? – Dzje Nov 23 '16 at 23:09
  • You sir are a very nice guy, much appreciated. – Dzje Nov 23 '16 at 23:56
  • `isset() && !empty()` is an antipattern which should not exist in any code for any reason. [Why check both isset() and !empty()](https://stackoverflow.com/a/4559976/2943403) – mickmackusa Jan 30 '22 at 08:31