Reeksy Posted October 6, 2006 Share Posted October 6, 2006 HiI have a line of code within a text file. Is it possible to search the file for that line of code and then return it?Jon Quote Link to comment Share on other sites More sharing options...
Barand Posted October 6, 2006 Share Posted October 6, 2006 If you want a specific line number[code]<?php $file = file('data.txt'); // to get line 28 echo $file[27];?>[/code] Quote Link to comment Share on other sites More sharing options...
Reeksy Posted October 6, 2006 Author Share Posted October 6, 2006 OK thanks.Problem is i don't always know the line number! However i do know the text string.Is it possible to search the file for an instance of that text string? Quote Link to comment Share on other sites More sharing options...
printf Posted October 6, 2006 Share Posted October 6, 2006 How big is the file, or how big may the file become that you want to search? You can read line by line for big files and slurp for small files. So the size of your file will determine the best way to search within it!me! Quote Link to comment Share on other sites More sharing options...
Reeksy Posted October 6, 2006 Author Share Posted October 6, 2006 The file would never be bigger than 100K. Usually around 50K.Does that help!? Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 6, 2006 Share Posted October 6, 2006 so, you're simply searching for the occurence of a pre-defined string within a text file?[code]<?php$text = file_get_contents('myFile.txt');$search = "My Text";if (strpos($text, $search)) { // search string is in file} else { // not in the file}?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted October 6, 2006 Share Posted October 6, 2006 But what if the file begins with "My Text"? Quote Link to comment Share on other sites More sharing options...
rajmohan Posted October 6, 2006 Share Posted October 6, 2006 can you please tell me for what it will be use?? i mean i which situation you are using this please tell me Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 6, 2006 Share Posted October 6, 2006 [quote author=Barand link=topic=110671.msg447948#msg447948 date=1160137474]But what if the file begins with "My Text"?[/quote]i suppose that would call for a preg_match() or other type search at that point:[code]<?php$text = file_get_contents('myFile.txt');$search = "|My Text|i";if (preg_match($search, $text) { // search string is in file} else { // not in the file}?>[/code] Quote Link to comment Share on other sites More sharing options...
Reeksy Posted October 6, 2006 Author Share Posted October 6, 2006 [quote author=rajmohan link=topic=110671.msg447951#msg447951 date=1160137784]can you please tell me for what it will be use?? i mean i which situation you are using this please tell me[/quote]OK the scenario is: I have a link on my website, this link fires off to a map on an external website. However this external map service requires the coordinates of the item you’re looking at. The coordinates are specific to an item and can change so ideally they need to be dynamically pulled out.My problem is I don’t have the coordinates to pull out dynamically. Therefore I need to copy the exact link from another website which does have the up-to-date coordinates.So I need to get a line of code from their source code. I can get the contents using file_get_contents() I just need to know how to find the line where that link is and return it. I wont always know the line number but I do know the name of the link (which only appears once within the source code)Hope that makes sense! Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 6, 2006 Share Posted October 6, 2006 so, you're wanting to pull the line number of the matching text:[code]<?php$handle = fopen($file, 'r');$lMatch = -1; // default to a recognizable failure value$search = "|my text|i";if ($handle) { $num = 0; while (!feof($handle)) { $line = fgets($handle); $num++; // increment line number if (preg_match($search, $line)) { $lMatch = $num; break; } } fclose($handle);}if ($lMatch < 0) { // no matches} else { // match found: echo "Line $lMatch contains the search string";}?>[/code]now you just have to figure out if they want all matching line numbers if there are more than one. that would take some slight modification to the code above. Quote Link to comment Share on other sites More sharing options...
Reeksy Posted October 6, 2006 Author Share Posted October 6, 2006 Job Done!Ok thanks a lot guys. Much appreciated.I'm sure I'll be back soon!Jon Quote Link to comment 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.