Forum Moderators: coopster

Message Too Old, No Replies

strstr() Returns False When True?

         

brodyh

11:30 pm on Aug 29, 2009 (gmt 0)

10+ Year Member



I have a foreach() loop which outputs the name of an album (it's a music-related website) if its name is not in a string. However, when using strstr() in an if statement, it returns false regardless of whether or not the needle exists in the haystack. I already know the problem isn't case-related (I've even used stristr() even though the case should never change).

Has anyone had a similar problem? I will provide the code I am discussing below because I think the issue is specific to how I'm writing the code.

<?php

/*
This example finds albums from Blink-182 and Angels and Airwaves in the year 2005.
*/

// Emulate data from MySQL database
$Users['follow_artists'] = "0743b15a-3c32-48c8-ad58-cb325350befa;albums_singles,albums_ep,albums_studio,concerts#05071a7e-e71c-4306-a509-591cd3686893;albums_singles,albums_ep,albums_studio,concerts";

$follow_artists = explode("#", $Users['follow_artists']);

$echoed_albums = "";
foreach ($follow_artists as $artist) {
$follow_perim = explode(";", $artist);
$follow_mbid = $follow_perim[0];
$follow_args = $follow_perim[1];
if (stristr($follow_args, "albums_studio")) {
$mbquery = 'http://example.com/ws/1/release/?type=xml&releasetypes=Official&artistid=' . $follow_mbid;
$xmlmbstr = file_get_contents($mbquery) or die();
$xmlmb = new SimpleXMLElement($xmlmbstr) or die();
$Year = "2006";
foreach($xmlmb->{'release-list'}->release as $release) {
foreach($release->{'release-event-list'} as $releaselist) {
foreach($releaselist->event as $event) {
if (!empty($event['date'])) {
if (strstr($event['date'], $Year)) {
if (!strstr($echoed_albums, $release->title)) { // This returns false even when true. UGH!
echo $release->title . "<br/></br>";
$echoed_albums .= $release->title . ",";
}
}
}
}
}
}
}
}

?>

[edited by: dreamcatcher at 6:16 pm (utc) on Aug. 30, 2009]
[edit reason] use example.com. Thanks. [/edit]

andrewsmd

7:15 pm on Aug 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try if(stristr($echoed_albums, $release->title) !== false)

I don't know if that will fix your problem but I remember one time I had an issue of searching for a string and it was returning 0 because the string started at the begenning of the haystack and the if was reading this as if(0) and thus false when true. Other than that the i would make it case insensitive as I did, unless you need it. If that's not working and you know that $release->title is in the string do a var_dump on both $release->title and $echoed_albums to make sure they are both strings of what you think. If that is the case then use
if(strpos($haystack, $needle) !== false);
That will work for sure I know.