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 Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/ 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. Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491427 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 Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491441 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. Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491444 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" Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491447 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 Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491456 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!'; ?> Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491462 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. Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491463 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. Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491464 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) Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491481 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 Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491502 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. Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491504 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 Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491506 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 Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491565 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"; Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491597 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 Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491646 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 Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491653 Share on other sites More sharing options...
discomatt Posted March 13, 2008 Share Posted March 13, 2008 no problemo Link to comment https://forums.phpfreaks.com/topic/95992-txt-file-csv/#findComment-491658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.