| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hi, I tried searching myself using google and on php.net, but too much irrelevant answers and not a single one helpful so please bare with me. Could someone please show me how to get one line from a file, much like unix "grep '^needle' haystack" ? I'm sure there are zero or one matches, and I don't need fancy regular expressions, just a string to be found at the beginning of a line. That string won't be the entire line though. TIA |
|
#2
| |||
| |||
| |
|
#3
| |||
| |||
| mijn naam wrote: > Hi, > > I tried searching myself using google and on php.net, but too much > irrelevant answers and not a single one helpful so please bare with me. > > Could someone please show me how to get one line from a file, much like > unix "grep '^needle' haystack" ? > > I'm sure there are zero or one matches, and I don't need fancy regular > expressions, just a string to be found at the beginning of a line. That > string won't be the entire line though. > > TIA > You'll have to do it like grep does - read the file in and compare. The easiest would be to read in a line at a time with fgets(). Alternatively, you could read in a block at a time and parse the block into lines. More work for you but it could be faster. Of course, a database would make things much easier :-) -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
#4
| |||
| |||
| On Wed, 27 Aug 2008 01:01:10 +0200, "mijn naam" <whatever@hotmail.invalid> wrote: >Hi, > >I tried searching myself using google and on php.net, but too much >irrelevant answers and not a single one helpful so please bare with me. > >Could someone please show me how to get one line from a file, much like unix >"grep '^needle' haystack" ? > >I'm sure there are zero or one matches, and I don't need fancy regular >expressions, just a string to be found at the beginning of a line. That >string won't be the entire line though. $line = `grep '^needle' haystack`; ![]() -- gburnore at DataBasix dot Com --------------------------------------------------------------------------- How you look depends on where you go. --------------------------------------------------------------------------- Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ .. | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ Official .sig, Accept no substitutes. | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ .. | ÝÛ 0 1 7 2 3 / Ý³Þ 3 7 4 9 3 0 Û³ Black Helicopter Repair Services, Ltd.| Official Proof of Purchase ================================================== ========================= |
|
#5
| |||
| |||
| Gary L. Burnore schreef: > On Wed, 27 Aug 2008 01:01:10 +0200, "mijn naam" > <whatever@hotmail.invalid> wrote: > >> Hi, >> >> I tried searching myself using google and on php.net, but too much >> irrelevant answers and not a single one helpful so please bare with me. >> >> Could someone please show me how to get one line from a file, much like unix >> "grep '^needle' haystack" ? >> >> I'm sure there are zero or one matches, and I don't need fancy regular >> expressions, just a string to be found at the beginning of a line. That >> string won't be the entire line though. > > > > $line = `grep '^needle' haystack`; Regrettably, we are not all on *nix. ;-) Regards, Erwin Moller -- ============================ Erwin Moller Now dropping all postings from googlegroups. Why? http://improve-usenet.org/ ============================ |
|
#6
| |||
| |||
| "mijn naam" <whatever@hotmail.invalid> schreef in bericht news:6d5cb$48b48b34$53565551$17128@cache100.multik abel.net... > Hi, > > I tried searching myself using google and on php.net, but too much > irrelevant answers and not a single one helpful so please bare with me. > > Could someone please show me how to get one line from a file, much like > unix "grep '^needle' haystack" ? > > I'm sure there are zero or one matches, and I don't need fancy regular > expressions, just a string to be found at the beginning of a line. That > string won't be the entire line though. Thanks all. As expected there are many solutions. I got one from elsewhere as well. Part of the problem (which I did not mention) is that I needed only the rest of the line. So, for "xyz:abc" I'm only interested in "abc". The following function does this. What do you think of it? The file is: aaa:xyz xyz:abc bbb qr[...] function match($what,$where) { $x=file_get_contents($where); $a=explode("\n",$x); $b=preg_grep("/^{$what}:/",$a); if (count($b)!=1) return FALSE; $b=implode("",$b); $b=explode(":",$b); if (count($b)!=2) return FALSE; $b=$b{1}; return $b; } |
|
#7
| |||
| |||
| mijn naam wrote: > "mijn naam" <whatever@hotmail.invalid> schreef in bericht > news:6d5cb$48b48b34$53565551$17128@cache100.multik abel.net... >> Hi, >> >> I tried searching myself using google and on php.net, but too much >> irrelevant answers and not a single one helpful so please bare with me. >> >> Could someone please show me how to get one line from a file, much >> like unix "grep '^needle' haystack" ? >> >> I'm sure there are zero or one matches, and I don't need fancy regular >> expressions, just a string to be found at the beginning of a line. >> That string won't be the entire line though. > > > Thanks all. As expected there are many solutions. I got one from > elsewhere as well. > > Part of the problem (which I did not mention) is that I needed only the > rest of the line. > So, for "xyz:abc" I'm only interested in "abc". The following function > does this. What do you think of it? > > The file is: > aaa:xyz > xyz:abc > bbb qr> [...] > > > function match($what,$where) { > $x=file_get_contents($where); > $a=explode("\n",$x); > $b=preg_grep("/^{$what}:/",$a); > if (count($b)!=1) return FALSE; > $b=implode("",$b); > $b=explode(":",$b); > if (count($b)!=2) return FALSE; > $b=$b{1}; > return $b; > } I think this is MUCH better suited to a database with a call like $sql = "select the_column from table_foo where the_key like 'what'"; and where the_key is your aaa, bbb, etc. and the_column contains what you have now on the rest of the line after the ";". You can easily export your current files into a database table. |
|
#8
| |||
| |||
| <sheldonlg> schreef in bericht news:meidnUPvyoF5sCjVnZ2dnUVZ_j-dnZ2d@giganews.com... > I think this is MUCH better suited to a database with a call like > > $sql = "select the_column from table_foo where the_key like 'what'"; > > and where the_key is your aaa, bbb, etc. and the_column contains what you > have now on the rest of the line after the ";". > > You can easily export your current files into a database table. If that php script would be the only user of the file, you would probably have a good point. Unfortunately that's not the case. Thanks anyway! |
|
#9
| |||
| |||
| On Aug 27, 2:43 pm, "mijn naam" <whate...@hotmail.invalid> wrote: > <sheldonlg> schreef in berichtnews:meidnUPvyoF5sCjVnZ2dnUVZ_j-dnZ2d@giganews.com... > > > I think this is MUCH better suited to a database with a call like > > > $sql = "select the_column from table_foo where the_key like 'what'"; > > > and where the_key is your aaa, bbb, etc. and the_column contains what you > > have now on the rest of the line after the ";". > > > You can easily export your current files into a database table. > > If that php script would be the only user of the file, you would probably > have a good point. Unfortunately that's not the case. > > Thanks anyway! Or you could use something like this. function find_thing($what,$where) { $temp=file_get_contents($where); if (!$start=strpos($temp,$what)) return false; else $start+=strlen($what)+1; //Add +1 because of the : $end=strpos($temp,"\n",$start); return substr($temp,$start,$end); } |
|
#10
| |||
| |||
| "George Maicovschi" <georgemaicovschi@gmail.com> schreef in bericht news:2a56182a-4b0e-43f4-b671-2166989d85b1@d45g2000hsc.googlegroups.com... > Or you could use something like this. > > function find_thing($what,$where) > { > $temp=file_get_contents($where); > if (!$start=strpos($temp,$what)) > return false; > else > $start+=strlen($what)+1; //Add +1 because of the : > $end=strpos($temp,"\n",$start); > return substr($temp,$start,$end); > } Looks good, thanks. It will need to be adjusted slightly, else "abc" would also match "abcd qrs" and return " qrs". It seems that appending ":" to"$what" is enough. Do you agree that using "file_get_contents" is more efficient than "while not found and not eof do fgets ..." ? |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.