Jump to content

plz help


Craziest

Recommended Posts

am trying to use the preg_match .. but it seems no working ... wt am tryin to do is to pass a variable then do search on wtever has been inserted... i wanted preg_match to look and match the inserted text through a file ...
the files name:
form.php
searching.php
data.txt
============
[code]
//searching.php
<?php


$search=$_POST["search"];  // from form.php


$lines = file('data.txt');
foreach ($lines as $line_num => $line) {

  if (preg_match('/('.$search.')/i', $line)) {
    //echo "<br> Line ", $line_num, " matches: ",$matches[0],"<br>";
    echo $line;
  }
}

?>
[/code]
lets say i inserted the word "php".... so the code is supposed to match the word "php" and print it in each line ....but it doesnt work and it shows the whole file lines
any help would be appreciated 
Link to comment
Share on other sites

Why dont you do it threw MySQL way easier then you can just do:

$search = $_POST['search'];

$submit = $_POST['submit'];

if(isset($submit)){

$results = mysql_query("SELECT * FROM data WHERE data_info LIKE '%$search%' ") or die(mysql_error());
while($show_results = mysql_fetch_array($results)){
echo $show_results[info];

Link to comment
Share on other sites

Your code works fine for me...

Using this:
[code]<?php
$search="php";  // I've hard coded
$lines = file('data.txt');
foreach ($lines as $line_num => $line) {

  if (preg_match('/('.$search.')/i', $line)) {
    //echo "<br> Line ", $line_num, " matches: ",$matches[0],"<br>";
    echo "$line<br>\n";
  }
}
?>[/code]

And a data file that looks like this:
[code]
This is a test
I like programming in php
I love php
CGI isn't easy
PHP rocks
Perl is better
[/code]

I get the following output:
[code]
I like programming in php
I love php
PHP rocks
[/code]

Regards
Huggie
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.