XeroXer Posted November 28, 2006 Share Posted November 28, 2006 Hi there!I have a textfile containing different information strings.The content is listed like this:[code]abcd:dcba:12344321:efgh:hgfe5678:ijkl:mnopponm:8765:lkji[/code]... Well you get the point.I want to be able to search with a php script in this file.The row/rows containing the search string should be listed by the script.So if I would have searched in the above file for [color=red]5678[/color] I would get this result listed:[code]5678:ijkl:mnop[/code]But as I wrote is must be able to handle multiple rows containing the string.I also want so that the different parts of the search string should be divided by some colorcode.Like this:[color=red]5678[/color]:[color=blue]ijkl[/color]:[color=green]mnop[/color]Can anyone do this or help me with the commands to do it myself? Quote Link to comment Share on other sites More sharing options...
Orio Posted November 28, 2006 Share Posted November 28, 2006 I guess that's not the fastest way, but it'll do :)[code]<?php//$text holds the value of the text file as a string (IE using file_get_contents() or anything simillar)$regex = "/([a-z0-9]+):(5678):([a-z0-9]+)|(5678):([a-z0-9]+):([a-z0-9]+)|([a-z0-9]+):([a-z0-9]+):(5678)/is";preg_match_all($regex,$text,$matches);foreach($matches[0] as $row){ $temp = explode(":",$row); echo "First = ".$temp[0]."<br>Second = ".$temp[1]."<br>Third = ".$temp[2]."<br><hr><br>";}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 28, 2006 Author Share Posted November 28, 2006 I can't get that to work.I did like this:[code]<?php$text = file_get_contents('file.txt');$regex = "/([a-z0-9]+):(5678):([a-z0-9]+)|(5678):([a-z0-9]+):([a-z0-9]+)|([a-z0-9]+):([a-z0-9]+):(5678)/is";preg_match_all($regex,$text,$matches);foreach($matches[0] as $row){ $temp = explode(":",$row); echo "First = ".$temp[0]."<br>Second = ".$temp[1]."<br>Third = ".$temp[2]."<br><hr><br>";}?>[/code]and it prints out nothing... Quote Link to comment Share on other sites More sharing options...
Orio Posted November 28, 2006 Share Posted November 28, 2006 Works fine for me.... Are you sure file.txt exists and $text receives the data all right?(Add after "$text = file_get...." the line "echo $text" and see if there's any output)Orio. Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 28, 2006 Author Share Posted November 28, 2006 If I type echo $text I get all the file content printed out yes.Is there any limitations regarding what letters, numbers och signs that can be used in the file?Because now the first part consists of just letters (small and large) and number.The second and third part is just about any sign you can get into a filename.For example: []()$-_ Quote Link to comment Share on other sites More sharing options...
Orio Posted November 28, 2006 Share Posted November 28, 2006 Yes, I assummed all the charaters are alphanumeric. Here is another version that can match all characters (but I added a limition of 4 charaters for each one, I hope that's how the file looks like too- at least it seemed so from the example):[code]<?php$text = file_get_contents('file.txt');$regex = "/(.{4}):(5678):(.{4})|(5678):(.{4}):(.{4})|(.{4}):(.{4}):(5678)/is";preg_match_all($regex,$text,$matches);foreach($matches[0] as $row){ $temp = explode(":",$row); echo "First = ".$temp[0]."<br>Second = ".$temp[1]."<br>Third = ".$temp[2]."<br><hr><br>";}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 28, 2006 Author Share Posted November 28, 2006 Actually no. :)The lenght differs from row to row.I think the longest infostring so faar is 55 characters. ::) Quote Link to comment Share on other sites More sharing options...
Orio Posted November 28, 2006 Share Posted November 28, 2006 lolLet's try now, I think this will work this time after you confusing me :P[code]$regex = "/\n(.*?):(5678):(.*?)\n|(5678):\n(.*?):(.*?)\n|\n(.*?):(.*?):(5678)\n/is";[/code]Orio. Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 28, 2006 Author Share Posted November 28, 2006 this is driving me crazy.my code now looks like this:[code]<?php$text = file_get_contents('file.txt');$regex = "/\n(.*?):(5678):(.*?)\n|(5678):\n(.*?):(.*?)\n|\n(.*?):(.*?):(5678)\n/is";preg_match_all($regex,$text,$matches);foreach($matches[0] as $row){ $temp = explode(":",$row); echo "First = ".$temp[0]."<br>Second = ".$temp[1]."<br>Third = ".$temp[2]."<br><hr><br>";}?>[/code]And I get no result.I also noticed that when I echo the code it doesn't show the rowchanges I have in my .txt file.It just prints it all out with automatic rowchange here and there... Quote Link to comment Share on other sites More sharing options...
Orio Posted November 28, 2006 Share Posted November 28, 2006 Dont worry about that, you can use the nl2br() function if you want to show it nicely.I now understand the problem :) I had one of the "\n" out of place :)[code]$regex = "/\n(.*?):(5678):(.*?)\n|\n(5678):(.*?):(.*?)\n|\n(.*?):(.*?):(5678)\n/is";[/code]Orio. Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 28, 2006 Author Share Posted November 28, 2006 I want to smash my computer to bits.Still won't work........ Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 28, 2006 Author Share Posted November 28, 2006 Who won't this output anything?[code]<?php$text = file_get_contents('file.txt');$text = nl2br($text);$regex = "/\n(.*?):(c944):(.*?)\n|\n(c944):(.*?):(.*?)\n|\n(.*?):(.*?):(c944)\n/is";preg_match_all($regex,$text,$matches);foreach($matches[0] as $row){ $temp = explode(":",$row); echo "First = ".$temp[0]."<br>Second = ".$temp[1]."<br>Third = ".$temp[2]."<br><hr><br>";}?>[/code] Quote Link to comment Share on other sites More sharing options...
Orio Posted November 28, 2006 Share Posted November 28, 2006 Try increasing your error reporting and see if there were errors. At the start of the script add:error_reporting(E_ALL);Orio.PSgtg now, sorry about the fact we havent solved your problem yet, altough I think we made progress :) Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 28, 2006 Author Share Posted November 28, 2006 Added that row and still I get nothing printed out.Is there any character that would destroy this script?Make it not being able to search?PSIf you have time tomorrow I would appreciate it if you would help me then. :-) Quote Link to comment Share on other sites More sharing options...
Orio Posted November 29, 2006 Share Posted November 29, 2006 This is weird, can you send me the file or something (or upload it in here).Orio. Quote Link to comment Share on other sites More sharing options...
Zane Posted November 29, 2006 Share Posted November 29, 2006 might work...no guarentees but worth a try[code]<?php$lines = file('file.txt');$searchFor = '5678';$results = array();foreach($lines as $line) { if(ereg($searchFor, $line); $cols = explode(":", $line); $color = array('red','greed','blue'); for($x=0;$x<count($cols);$x++) { $cols[$x] = printf("<font color='%s'>{$cols[$x]}</p>", $color); } $line = implode(":", $cols); $results[] = $line; }print_r($results, true);?>[/code] Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 29, 2006 Author Share Posted November 29, 2006 I can give you an example of the rows...[code]b1ed9d52b23577a036c9591cdac0194a:counter-strike(v.3).gif:[C]ounter-[S]trike game loader version 1$2. (gif)[/code]zanus:[code]Parse error: syntax error, unexpected ';' in /customers/xeroxer.com/xeroxer.com/httpd.www/_/-/search/test.php on line 8[/code] Quote Link to comment Share on other sites More sharing options...
Orio Posted November 29, 2006 Share Posted November 29, 2006 Remove the ; from:if(ereg($searchFor, $line);Orio. Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 29, 2006 Author Share Posted November 29, 2006 [quote author=Orio link=topic=116606.msg475712#msg475712 date=1164820112]Remove the ; from:if(ereg($searchFor, $line);Orio.[/quote]Then I get:[code]Parse error: syntax error, unexpected T_VARIABLE in /customers/xeroxer.com/xeroxer.com/httpd.www/_/-/search/test.php on line 9[/code]line 9 being:[code]$cols = explode(":", $line);[/code] Quote Link to comment Share on other sites More sharing options...
Orio Posted November 29, 2006 Share Posted November 29, 2006 Using zanus's code, bugs fixed:[code]<?php$lines = file('file.txt');$searchFor = '5678';$results = array();$color = array('red','greed','blue');foreach($lines as $line){ if(ereg($searchFor, $line) { $cols = explode(":", $line); for($x=0;$x<count($cols);$x++) $cols[$x] = sprintf("<font color='%s'>{$cols[$x]}</p>", $color[$x]); $line = implode(":", $cols); $results[] = $line; }}print_r($results, true);?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 29, 2006 Author Share Posted November 29, 2006 [code]Parse error: syntax error, unexpected '{' in /customers/xeroxer.com/xeroxer.com/httpd.www/_/-/search/test.php on line 11[/code] Quote Link to comment Share on other sites More sharing options...
Zane Posted November 29, 2006 Share Posted November 29, 2006 there's a missing ' ) '...here[quote]if(ereg($searchFor, $line)[b])[/b][/quote] Quote Link to comment Share on other sites More sharing options...
XeroXer Posted November 29, 2006 Author Share Posted November 29, 2006 Changed that now.Get no error but still no output from the file. 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.