Craziest Posted October 11, 2006 Share Posted October 11, 2006 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.phpdata.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 Quote Link to comment https://forums.phpfreaks.com/topic/23630-plz-help/ Share on other sites More sharing options...
Demonic Posted October 11, 2006 Share Posted October 11, 2006 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]; Quote Link to comment https://forums.phpfreaks.com/topic/23630-plz-help/#findComment-107271 Share on other sites More sharing options...
Craziest Posted October 11, 2006 Author Share Posted October 11, 2006 Thank you Demonic ...but i want it in that way ...any suggestions Quote Link to comment https://forums.phpfreaks.com/topic/23630-plz-help/#findComment-107357 Share on other sites More sharing options...
HuggieBear Posted October 11, 2006 Share Posted October 11, 2006 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 testI like programming in phpI love phpCGI isn't easyPHP rocksPerl is better[/code]I get the following output:[code]I like programming in phpI love phpPHP rocks[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23630-plz-help/#findComment-107367 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.