Jump to content


Photo

plz help


  • Please log in to reply
3 replies to this topic

#1 Craziest

Craziest
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 11 October 2006 - 03:07 AM

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
============
//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;
  }
}

?>
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 

#2 Demonic

Demonic
  • Members
  • PipPipPip
  • Advanced Member
  • 562 posts

Posted 11 October 2006 - 03:23 AM

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];



#3 Craziest

Craziest
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 11 October 2006 - 10:55 AM

Thank you  Demonic ...
but i want it in that way ...
any suggestions

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 11 October 2006 - 11:11 AM

Your code works fine for me...

Using this:
<?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";
  }
}
?>

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

I get the following output:
I like programming in php
I love php
PHP rocks

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users