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
https://forums.phpfreaks.com/topic/23630-plz-help/
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
https://forums.phpfreaks.com/topic/23630-plz-help/#findComment-107271
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
https://forums.phpfreaks.com/topic/23630-plz-help/#findComment-107367
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.