Name: Anonymous 2014-01-09 23:19
this seems like it should be really easy but it's giving me a lot of trouble
at this point I'm trying to get the number of tracks for a given release on the website RateYourMusic.com
all the code for the PHP file is at the end of my post
the part that says...
$searchFor = '<span class="tracklist_num">'
...finds every number that's a track number
and the part that says...
$trackNumbers = max($trackNumbers)
finds the highest of those numbers (e.g. the total number of tracks)
the problem is that that number comes up several times on the page
so if the release has 12 tracks I get the string "12" over and over again
I tried putting them into an array and then getting the FIRST element of the FIRST array
but for some reason that gives me the same number of single-element arrays (with the string "1" in them) as total track numbers
(in other words, it gives me as many single-element arrays [with the string "1" in them] as I would get "12"s in the above example)
I feel like I'm overlooking something obvious but I'm pretty new to this so let me know if you can see what I'm doing wrong
here's all the code:
<?php
$searchFor = '<span class="track_rating';
$line = file_get_contents($_FILES["file"]["tmp_name"]);
$pattern = preg_quote($searchFor, '/');
$pattern = "/^.*$pattern.*\$/m";
if (preg_match_all($pattern, $line, $matches)) {
echo implode("<br> \n", $matches[0]);
}
else {
echo "No track ratings found.";
}
$searchFor = '<span class="tracklist_num">';
$line = file_get_contents($_FILES["file"]["tmp_name"]);
$pattern = preg_quote($searchFor, '/');
$pattern = "/^.*$pattern.*\$/m";
while (preg_match_all($pattern, $line, $matches)) {
foreach ($matches as $trackNumbers) {
$trackNumbers = preg_replace('/\D/', '', $trackNumbers);
}
$trackNumbers = max($trackNumbers);
$trackNumbers = array($trackNumbers);
$trackNumbers = $trackNumbers[0][0];
var_dump($trackNumbers);
}
?>
at this point I'm trying to get the number of tracks for a given release on the website RateYourMusic.com
all the code for the PHP file is at the end of my post
the part that says...
$searchFor = '<span class="tracklist_num">'
...finds every number that's a track number
and the part that says...
$trackNumbers = max($trackNumbers)
finds the highest of those numbers (e.g. the total number of tracks)
the problem is that that number comes up several times on the page
so if the release has 12 tracks I get the string "12" over and over again
I tried putting them into an array and then getting the FIRST element of the FIRST array
but for some reason that gives me the same number of single-element arrays (with the string "1" in them) as total track numbers
(in other words, it gives me as many single-element arrays [with the string "1" in them] as I would get "12"s in the above example)
I feel like I'm overlooking something obvious but I'm pretty new to this so let me know if you can see what I'm doing wrong
here's all the code:
<?php
$searchFor = '<span class="track_rating';
$line = file_get_contents($_FILES["file"]["tmp_name"]);
$pattern = preg_quote($searchFor, '/');
$pattern = "/^.*$pattern.*\$/m";
if (preg_match_all($pattern, $line, $matches)) {
echo implode("<br> \n", $matches[0]);
}
else {
echo "No track ratings found.";
}
$searchFor = '<span class="tracklist_num">';
$line = file_get_contents($_FILES["file"]["tmp_name"]);
$pattern = preg_quote($searchFor, '/');
$pattern = "/^.*$pattern.*\$/m";
while (preg_match_all($pattern, $line, $matches)) {
foreach ($matches as $trackNumbers) {
$trackNumbers = preg_replace('/\D/', '', $trackNumbers);
}
$trackNumbers = max($trackNumbers);
$trackNumbers = array($trackNumbers);
$trackNumbers = $trackNumbers[0][0];
var_dump($trackNumbers);
}
?>