shage Posted March 13, 2008 Share Posted March 13, 2008 Was wondering if possible to use a GET_ say like shoes, and it search the csv and only echo out the lines that have the word shoes in it Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 Yes, regex would be the best method for this. Alternately, you could also use strstr or stristr, but they are less flexible. Quote Link to comment Share on other sites More sharing options...
shage Posted March 13, 2008 Author Share Posted March 13, 2008 thank you now time to find a example Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 Well, did you want to load up a text file, parse it line by line, and populate an array with the entire line that contains string 'x' ? Â i can help you with an example, just really not sure what you need to do. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 13, 2008 Share Posted March 13, 2008 "use a GET_ say like shoes, and it search the csv and only echo out the lines that have the word shoes in it" Quote Link to comment Share on other sites More sharing options...
shage Posted March 13, 2008 Author Share Posted March 13, 2008 i have a csv that is broken into arrays one section being the description of the links, so if the user comes from the search term shoes, i would like it to get the get_ term=shoes and parse the csv for any link with shoes in the description, hope that makes more sense Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 I'm not sure what a GET_ is. And making assumptions is never a good idea.  anyways, here's how i'd do it  <?php function parse_csv($file, $search) {  if (!$fArr = file($file) )    return FALSE;  foreach($fArr as $line)    if (strpos($line, $search) !== FALSE)     $ret[] = $line;  if (is_array($ret) )    return $ret;  else    return FALSE; } $array = parse_csv('path/to/csv.txt', 'shoes'); if ($array)  foreach ($array as $value)    echo $value; else  echo 'no lines found!'; ?> Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 I can give you a more specific example if you show me a sample of the csv file you're working with. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 13, 2008 Share Posted March 13, 2008 i have a csv that is broken into arrays one section being the description of the links, so if the user comes from the search term shoes, i would like it to get the get_ term=shoes and parse the csv for any link with shoes in the description, hope that makes more sense It would be very helpful if you provided a few lines of data from the text file and explained exactly what you want returned. Without knowing the format of the input and output it is difficult, if not impossible, to provide a good response. Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 Also, in the function I provided above, if you'd like to make it case insensitive, change this line     if (strpos($line, $search) !== FALSE)  to this     if (strpos(strtolower($line), strtolower($search)) !== FALSE) Quote Link to comment Share on other sites More sharing options...
shage Posted March 13, 2008 Author Share Posted March 13, 2008 http://www.nike.com,Nike,Best Shoes  thats a example of one of my links in the csv, url, name, descript Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 So you just want to extract the url? Â My function should work fine for what you are doing. Quote Link to comment Share on other sites More sharing options...
shage Posted March 13, 2008 Author Share Posted March 13, 2008 header('Content-type: text/xml'); echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; //remove space after ? (added for make it format in here) echo "<rss version=\"2.0\">\n"; echo "Â <channel>\n"; echo "Â Â <title>$site</title>\n"; echo "Â Â <link>http://www.yahoo.com/</link>\n"; echo "Â Â <description>Feeds</description>\n"; $row = 1; while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) { $link = $data[0]; $description = $data[1]; $thumb= $data[2]; echo "Â Â <item>\n"; echo "Â Â Â <title>$description</title>\n"; echo "Â Â Â <link>$shage2</link>\n"; echo "Â Â Â <description><![CDATA[<img src=\"$thumb\"]]>></description>\n"; echo "Â Â </item>\n"; } fclose($handle); echo "Â </channel>\n"; echo "</rss>\n"; Â that is the code im trying to loop the results to Quote Link to comment Share on other sites More sharing options...
shage Posted March 13, 2008 Author Share Posted March 13, 2008 thanks to those who have helped so far Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 Easy...  add this function somewhere in your code  function parse_csv($file, $search) {  if (!$fArr = file($file) )    return FALSE;  foreach($fArr as $line)    if (strpos( strtolower($line), strtolower($search) ) !== FALSE)     $ret[] = explode(',', $line);  if (is_array($ret) )    return $ret;  else    return FALSE; }  and then call your rss like this  echo "  <description>Feeds</description>\n"; $arr = parse_csv('path/to/csv.txt', 'shoes'); foreach ($arr as $data) { echo "  <item>\n"; echo "   <title>$data[1]</title>\n"; echo "   <link>$data[0]</link>\n"; echo "   <description><![CDATA[<img src=\"$thumb\"]]>></description>\n"; echo "  </item>\n"; } echo " </channel>\n"; Quote Link to comment Share on other sites More sharing options...
shage Posted March 13, 2008 Author Share Posted March 13, 2008 some reason it just echos t over and over Quote Link to comment Share on other sites More sharing options...
shage Posted March 13, 2008 Author Share Posted March 13, 2008 got it thank you thank you thank you Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 no problemo Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.