Re: PHP Search & Links



On Sat, 17 Sep 2005 21:53:29 +0300, Dimitris Mexis <m65@xxxxxxxxxxxx>
wrote:

>Tyrone Slothrop wrote:
> > On Sat, 17 Sep 2005 14:52:11 +0300, Dimitris Mexis <dmexis@xxxxxxxxx>
> > 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;
}
}


.



Relevant Pages

  • Re: how to ignore the first 2 items in this iteraction?
    ... //using another array because we only want ... echo implode; ... Otherwise first time around nothing's been assigned to $results because ... Probably needs a lot of tweaking, but our job is to simply point the ...
    (comp.lang.php)
  • Re: Keeping score
    ... Sorry for misunderstanding the first time. ... you are adding one to numCorrect or numIncorrect no matter ... I have tried using the qAnswered array found on the site, ... Dim visitedAs Boolean ...
    (microsoft.public.powerpoint)
  • Strange "Stack Overflow" message occurring with use of vector
    ... this is the first time I've posted to it, ... with a dynamic array). ... binary chains stored consecutively in the vector at each iteration), ... the code breaks with a "Stack Overflow" error message. ...
    (microsoft.public.vc.language)
  • Re: Dynamic Form
    ... customer will answer all 5 questions then press next. ... I have read the PHP manual pages. ... echo Age; ... only other thought is that for some reason, I can't access the array ...
    (comp.lang.php)
  • Re: Referencing Korn Shell Array Names as a Variable
    ... Your idea worked for displaying the output from the command. ... $ echo $ ... # Attempt to use variable array name and index to reference array value ... The issue is with this line of the script ...
    (comp.unix.shell)