|
Posted by Tyrone Slothrop on 09/18/05 08:13
On Sat, 17 Sep 2005 21:53:29 +0300, Dimitris Mexis <m65@vivodinet.gr>
wrote:
>Tyrone Slothrop wrote:
> > On Sat, 17 Sep 2005 14:52:11 +0300, Dimitris Mexis <dmexis@sparks.gr>
> > wrote:
> >
> >
> >>I have created a small script that searches a word with eregii through a
> >>page. Assume that it found the word, and reports me the string,
> >>1) Can I take the line of this word is shown...?
> >>2) Can I create a link to this part of this page?
> >
> >
> > You may want to use the file() function which treats each line of a
> > file as an array.
> > $lines = file('/path/to/file');
> > Then find which line numbers contain the word:
> > foreach ($lines as $line_num => $line) {
> > if (/*Your regex here run on $line*/) {
> > $match[] = $line_num;
>You initialize an array, without declaring and without specifying limits?!?!
>Isn't this untrusted for memory leak?I mean this could be very
>unpleasant overflow...right?
> > }
> > }
> > //Then display the lines
> > foreach ($match as $which) {
> > echo "a href=\"#$which\">".$lines[$match]."\<a>\";
> > }
>This last one does not function...the error is
>
>Parse error: parse error, unexpected $ in /var/www/html/seek_url.php3 on
>line 70
>and to tell you the truth it is the first time i see something like
>this, and i am new with php and this string-situation...i try to learn it...
>What do you suggest to do? I have replaced the $ at times or the echo I
>command I turned it to print... didn't work...
Oh, hell. I wrote this before I finished my first cup of coffee this
morning. It really wasn't meant as code to use so much as a guideline
for accomplishing what you need.
Instead of "\<a>\" it should read "</a>". I need stronger coffee or a
caffeine IV. ;-)
Anyway, the part displaying the lines should display all lines and
only replace those which match the word. That part should be more
like this:
foreach ($lines as $line_num=>$line) {
if (in_arrary ($line_num, $match)) {
echo "a href=\"#$line_num\">$line</a>";
} else {
echo $line;
}
}
[Back to original message]
|