Jump to content

Searching a file for string


XeroXer

Recommended Posts

Hi there!
I have a textfile containing different information strings.
The content is listed like this:
[code]abcd:dcba:1234
4321:efgh:hgfe
5678:ijkl:mnop
ponm: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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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: []()$-_
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.