Reputation: 604
I am working on a schedule system that collects information from a RSS feed. However, when I try to strip the necessary segment from the RSS preg_match returns it as invalid.
//Finds the day by stripping data from the myDragonnet RSS feed.
function schedule($given_date) {
$url = "http://mydragonnet.hkis.edu.hk/schedule/day_schedule_rss.php?schedule_id=1";
$rss = simplexml_load_file($url);
$date = date("~jS M Y~", strtotime($given_date));
if($rss) {
foreach($rss->channel->item as $item) {
foreach ($item->title as $story) {
if (strpos($date, $story) !== false) {
preg_match("/Day (\d+)/", $story, $m);
break; // stop searching
}
}
}
}
return $m[1];
}
The function : <?php echo schedule('01/24/2010'); ?>
This is the error I'm getting -
Warning: preg_match() [function.preg-match]: Unknown modifier '/' in ***/class.schedule.php on line 31
Upvotes: 1
Views: 433
Reputation: 66
try this:
// Finds the day by stripping data from the myDragonnet RSS feed.
function schedule($given_date) {
$url = "http://mydragonnet.hkis.edu.hk/schedule/day_schedule_rss.php?schedule_id=1";
$rss = simplexml_load_file($url);
$date = date("jS M Y", strtotime($given_date));
$found = false;
if($rss) {
foreach($rss->channel->item as $item) {
foreach ($item->title as $story) {
if (strpos($story, $date) !== false) {
if (preg_match('/Day (\d+)/i', $story, $m)) {
$found = true;
break; // stop searching
}
}
}
if ($found)
{
break;
}
}
}
return $m[1];
}
Upvotes: 1
Reputation: 454990
The reason for this error is that preg_match
expects its first argument (the pattern) to be enclosed in pair of delimiters say /the pattern/
So change:
preg_match($date, $story)
to
preg_match('!'.preg_quote($date).'!', $story)
Also it looks like you are using preg_match
to just search for a string in another string. It is better to use a string searching function like strpos
or strstr
for such a thing:
if (strpos($date, $story) === false)
continue;
You can rewrite your foreach
loop as
foreach ($item->title as $story) {
if (strpos($date, $story) !== false) {
echo $story;
break; // stop searching
}
}
Upvotes: 2
Reputation: 15905
Why not use strstr()
instead of preg_match. It's much faster! (Documentation)
Upvotes: 1