Storm1
Storm1

Reputation: 209

how to not show duplicate text?

Code:

    $title = 'Ev 134'; 

    foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=601') as $f ) {
        if (strpos($f->title, $title) !== false)
            echo "<span style='font-weight: 600;'>". $f->title.":". "</span>". "<br>". $f->description. "<br>";
    }

    foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=604') as $f ) {
        if (strpos($f->title, $title) !== false)
            echo "<span style='font-weight: 600;'>". $f->title.":". "</span>". "<br>". $f->description. "<br>";
    }

output:

1. Ev 134 Seljestad - Horda, på strekningen Jøsendal - Røldal (Vestland) :
Description: Kolonnekjøring: Kolonnekjøring på grunn av uvær. Gjelder fra: 22.02.2020 20:18

2. Ev 134 Haukelifjell (Vestland/Vestfold og Telemark) :
Description: Midlertidig stengt: Stengt på grunn av uvær. Blir ikke åpnet i dag. Gjelder fra: 22.02.2020 09:16

3. Ev 134 Oslofjordtunnelen, på strekningen Drammen - Vassum (Viken) :
Description: Midlertidig stengt: Stengt i periodene: Mandag til fredag fra 07:00 til 09:00 og fra 15:00 til 18:00 for kjøretøy med totalvekt over 32 tonn. Gjelder fra: 22.01.2020 07:00 Gjelder til: 31.12.2020 18:00

4. Ev 134 Haukelifjell (Vestfold og Telemark/Vestland) :
Description: Midlertidig stengt: Stengt på grunn av uvær. Blir ikke åpnet i dag. Gjelder fra: 22.02.2020 09:16

the numbers (1-4) and "description:" tags ^^ in the output is added to make it easier to explain the problem.

Sometimes (because I gather info from two URLs) same text occurs multiple times. In this example: output nr. 2 and 4 are the same. How can I add a feature to prevent this from happening?

basically how to only show one title and description if two titles match 100%

any help is greatly appreciated!

Upvotes: 1

Views: 100

Answers (1)

Salines
Salines

Reputation: 5767

It's simple:

// new empty array
$feedToArray = [];

$title = 'Ev 134'; 

foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=601') as $f ) {
    if (strpos($f->title, $title) !== false) {
        $feedToArray[] = base64_encode(json_encode($f)); // <------ hash result in new array
    }
}

foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=604') as $f ) {
    if (strpos($f->title, $title) !== false) {
        $feedToArray[] = base64_encode(json_encode($f));
    }
}


$newArray = array_unique($feedToArray); // <--- remove duplicated hashed results

// loop new array
foreach($newArray as $hash ) {
    $f = json_decode(base64_decode($hash)); // <-- decode hash and use to echo.
        echo "<span style='font-weight: 600;'>". $f->title.":". "</span>". "<br>". $f->description. "<br>";
}

Edit:

added json_encode / json_decode object

Edit 2:

I am sorry, I thought the titles was 100% the same, but when I see now they aren't, any way to easily change this to search for the description instead of the title?

yes, use hashed description as array key, example

$feedToArray[base64_encode($f->description)] = base64_encode(json_encode($f));

But now you does not need array_unique($feedToArray);

And what now with titles?

Upvotes: 1

Related Questions