Charlie Trig
Charlie Trig

Reputation: 57

In PHP 7, How can I display results that have a specific word in the title?

just want to say thanks to this community for bailing me out countless times. Today I am trying to create a template override in a Joomla component to only display results that have "DE" in the title when the URL contains "/de/". I've tried a few things, but keep getting blank results. Here is what I have so far:

$keywords = " DE";
$title = JHtml::_('link', $link, $item->title); // Gets Pathway Title
if ((strpos($item, "DE") || strpos($_SERVER['REQUEST_URI'], "de")) == false) {

    $item = $displayData;

} else {

    $item = array_filter($displayData, function (array $item) use ($keywords) {
        return array_key_exists('title', $item) && $item['title'] === $keywords;
    });
}

Not sure how to get titles that contain " DE" at the end. Can anyone help me?

Upvotes: 0

Views: 42

Answers (1)

Barmar
Barmar

Reputation: 781130

You need to test each strpos() result separately, not combine them with ||. You also have to use === or !== when testing the result of strpos(), because loose comparison will treat 0 == false as true.

It's also easier to understand if you use positive conditions rather than negatives.

if (strpos($_SERVER['REQUEST_URI'], "de")) !== false && strpos($item, "DE") !== false) {
    $item = array_filter($displayData, function (array $item) use ($keywords) {
        return array_key_exists('title', $item) && strpos($item['title'], $keywords) !== false;
    });
} else {
    $item = $displayData;
}

Upvotes: 1

Related Questions