Professional Web Applications Themes

[PHP] REGULAR EXPRESSION HELP - PHP Development

John wrote: > I need to match a pattern, not in a single-line but from a HTML page, which > obviously has loads of lines. I need to match 2 lines from this HTML page: > 1) <HTML><TITLE>FirstVariable - Second Variable</TITLE></HTML>........ > 2) <TABLE><TD><TR>(newline) > ThirdVariable</TR></TD></TABLE>... > > I tried this code: > 1) preg_match("/<HTML><TITLE>(\S+) - (\S+)</TITLE></HTML>/", $html_page, > $variables); > 2) preg_match("/<TABLE><TD><TR>\n(\S+)</TR></TD></TABLE>/", $html_page, > $variables); > > The first 2 variables are matched into the $variables array but not the > third one. Sometimes when the third one is matched, it starts from where I > want it to ...

  1. #1

    Default Re: [PHP] REGULAR EXPRESSION HELP

    John wrote:
    > I need to match a pattern, not in a single-line but from a HTML page, which
    > obviously has loads of lines. I need to match 2 lines from this HTML page:
    > 1) <HTML><TITLE>FirstVariable - Second Variable</TITLE></HTML>........
    > 2) <TABLE><TD><TR>(newline)
    > ThirdVariable</TR></TD></TABLE>...
    >
    > I tried this code:
    > 1) preg_match("/<HTML><TITLE>(\S+) - (\S+)</TITLE></HTML>/", $html_page,
    > $variables);
    > 2) preg_match("/<TABLE><TD><TR>\n(\S+)</TR></TD></TABLE>/", $html_page,
    > $variables);
    >
    > The first 2 variables are matched into the $variables array but not the
    > third one. Sometimes when the third one is matched, it starts from where I
    > want it to start but takes all the text to the end of the HTML doent!
    Your "newline" may be \r\n or \r instead of just \n.

    --
    ---John Holmes...

    Amazon Wishlist: [url]www.amazon.com/o/registry/3BEXC84AB3A5E/[/url]

    PHP|Architect: A magazine for PHP Professionals [url]www.phparch.com[/url]




    John W. Holmes Guest

  2. #2

    Default RE: [PHP] REGULAR EXPRESSION HELP

    > -----Original Message-----
    > From: John [mailto:filmfindeircom.net]
    > Sent: 12 July 2003 07:31
    >
    > I need to match a pattern, not in a single-line but from a
    > HTML page, which
    > obviously has loads of lines. I need to match 2 lines from
    > this HTML page:
    > 1) <HTML><TITLE>FirstVariable - Second Variable</TITLE></HTML>........
    > 2) <TABLE><TD><TR>(newline)
    > ThirdVariable</TR></TD></TABLE>...
    >
    > I tried this code:
    > 1) preg_match("/<HTML><TITLE>(\S+) - (\S+)</TITLE></HTML>/",
    > $html_page,
    > $variables);
    > 2) preg_match("/<TABLE><TD><TR>\n(\S+)</TR></TD></TABLE>/",
    > $html_page,
    > $variables);
    >
    > The first 2 variables are matched into the $variables array
    > but not the
    > third one. Sometimes when the third one is matched, it starts
    > from where I
    > want it to start but takes all the text to the end of the
    > HTML doent!
    >
    > Any ideas? Is there any characters that I should have escaped that I
    > didn't?? All I can think of is that because the first line
    > that I want to
    > match is on the FIRST LINE of the html page, that matches.
    > But reg-ex can't
    > handle the next line as its way down the page????
    Firstly, your newsline may actually be any of \n, \r, or \r\n according as
    the file was built on a *nix, Mac or PC platform, so your regex should take
    account of this.

    Secondly, both of your examples should produce scads of errors attempting to
    p the regular expression, because you have unescaped slashes and your
    delimiters are also slashes -- so either this is not a direct cut-and-paste
    of what's actually in your script, or you're not letting on about something
    else! The fix for this one is either to escape the slashes that are
    actually part of the match, or use something other than / as your delimiter.

    Thirdly, as you've got double quotes around the regex, it would be advisable
    to double the backslashes themselves (to ensure PHP doesn't attempt to
    interpret any of its own backslash sequences) -- either that or use single
    quotes to enclose the regex.

    Fourthly, and perhaps most importantly, by default the * and + modifiers are
    "greedy" -- that is, they match as much as possible consistent with the
    whole match succeeding. With your first match, this doesn't matter as
    there's only one </TITLE> in the doent, so there's no ambiguity; with the
    second match, there could be any number of occurrences of </TR></TD></TITLE>
    in the doent, and the greedy matching of \S+ means that it will always be
    the *last* of these that is found. The way to counter this is to use one of
    the "ungreedy" modifiers in your regex.

    Taking all of these into account, you probably want something like:

    preg_match('!<TABLE><TD><TR>(\n|\r\n|\r)(\S+)</TR></TD></TABLE>!U',
    $html_page, $variables);

    Cheers!

    Mike

    ---------------------------------------------------------------------
    Mike Ford, Electronic Information Services Adviser,
    Learning Support Services, Learning & Information Services,
    JG125, James Graham Building, Leeds Metropolitan University,
    Beckett Park, LEEDS, LS6 3QS, United Kingdom
    Email: [email]m.fordlmu.ac.uk[/email]
    Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
    Mike Ford Guest

  3. #3

    Default PHP, server load and optimize


    Is there a way to execute a few continuous lines of PHP code before the
    engine load user defined functions from a PHP script? We need to find a
    way to controll server load. the code of the script itself has been
    optimized completely.

    thanks


    Jon Shoberg Guest

  4. #4

    Default RE: [PHP] PHP, server load and optimize

    Jon

    You should look at some of the caches available as these will help.
    Hotscripts has a good list:

    [url]http://www.hotscripts.com/cgi-bin/search.cgi?bool=AND&query=cache&catid=2[/url]

    as well as:

    [url]http://apc.communityconnect.com/[/url]

    and this one has quite a following: [url]http://www.turcksoft.com/en/e_mmc.htm[/url]

    Hope this helps

    Adrian
    sourceguardian.com


    > -----Original Message-----
    > From: Jon Shoberg [mailto:jonshoberg.net]
    > Sent: 14 July 2003 14:23
    > To: [email]php-generallists.php.net[/email]
    > Subject: [PHP] PHP, server load and optimize
    >
    >
    >
    > Is there a way to execute a few continuous lines of PHP code before the
    > engine load user defined functions from a PHP script? We need to find a
    > way to controll server load. the code of the script itself has been
    > optimized completely.
    >
    > thanks
    >
    >
    >
    > --
    > PHP General Mailing List (http://www.php.net/)
    > To unsubscribe, visit: http://www.php.net/unsub.php
    >
    >
    >
    >
    Adrian Teasdale Guest

Similar Threads

  1. Regular expression help
    By John in forum PERL Beginners
    Replies: 20
    Last Post: July 21st, 11:38 PM
  2. Regular expression for both first and last name?
    By Steve Grosz in forum Macromedia ColdFusion
    Replies: 1
    Last Post: June 26th, 12:35 AM
  3. help on regular expression
    By Madhu Reddy in forum PERL Beginners
    Replies: 5
    Last Post: January 28th, 01:31 AM
  4. regular expression help..
    By Matthew Vickers in forum PHP Development
    Replies: 15
    Last Post: September 16th, 10:54 AM
  5. Regular Expression....?
    By Rajeev Soni in forum ASP.NET General
    Replies: 8
    Last Post: July 24th, 04:46 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139