Web Hosting Forums

Page 1 of 2 1 2 LastLast
Results 1 to 15 of 16

This is a discussion on remote search in the Hosting Talk & Chit-chat forum
i have a form action on my site that displays a URL from another website. however, i only want a specific table of that site ...

  1. #1
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74

    remote search

    i have a form action on my site that displays a URL from another website. however, i only want a specific table of that site to be shown. how do i delete all the other contents of the webpage?

    thankyou

  2. #2
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    Put the webpage's html code in $html

    preg_match('(<table>.+</table>)', $html, $regs);

    Now the array $regs contains the html for the tables on the pages with the first table in $regs[1]. Take your pick.

    If it doesn't work a) check if they write TABLE in capitals by any chance. b) wait for Jason to come by and correct my poor skills on regular expressions.
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

  3. #3
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    Originally posted by Gwaihir
    b) wait for Jason to come by and correct my poor skills on regular expressions.
    He asked for it...

    Actually, Wim's not that far off with what he posted, but there are a couple issues...

    1. <table> usually have attributes inside them, unless the site uses CSS to the fullest extent (most don't).

    2. HTML is not case sensitive, so the site could be using <table>, <TABLE>, or even <tAbLe>.

    3. Searching for .+ will match ANY character one or more times, including the closing </table> tag.

    4. preg_match stops after the first match is found, so $regs[1] would show the contents of the first table, but $regs[2] would not show the contents of the second.

    Here's a case insensitive, attribute friendly, multi-table ready regexp:

    preg_match_all("/<table[^>]*>[^(<\/table>)]*</table>/i",
    $html, $regs);

    In this case, the [^>]*> says match any not ">" character inside the table tag zero or more times, followed by a single ">."

    The [^(<\/table>]* means match anything that's not a closing table tag.

    The "i" at the end of the pattern tells PHP that we want to do a case-insensitive search and using preg_match_all() will cause it keep searching the file until the end, no matter how many tables it finds.

    Also note that if the site uses tables within other tables (such as for layout purposes), this will fail. You'll need a more complex expression and probably a multi-pass recursive search to get around that.

    --Jason

    EDIT: BTW, this isn't tested, so it very well may be buggy.
    Last edited by jason; 06-22-2003 at 06:54 PM.
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  4. #4
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74

    re

    thanks for the reply. I have managed to cut the content recived down to what i need plus 2 tables that i don't need:

    |A|B|C|D|

    A,B,C and D represent 4 columns, and belows is a sample of the data for them (i need A and B, i don't need C and D);

    <td>A5 AAB</td><td>Auction</td><td><a href="javascript: showPlate('A5%20AAB'); return true;" onclick="return showPlate('A5%20AAB');" target="dvlaplate">View Plate</a></td><td><a href="emailafriend.php?plate=A5%20AAB">E-mail a Friend</a></td></tr><tr><td>A12 AAU</td><td>250</td><td><a href="javascript: showPlate('A12%20AAU'); return true;" onclick="return showPlate('A12%20AAU');" target="dvlaplate">View Plate</a></td><td><a href="emailafriend.php?plate=A12%20AAU"> E-mail a Friend</a></td></tr>.................

    .............<tr><td>A18 ACU</td><td>250</td><td><a href="javascript: showPlate('A18%20ACU'); return true;" onclick="return showPlate('A18%20ACU');" target="dvlaplate">View Plate</a></td><td><a href="emailafriend.php?plate=A18%20ACU"> E-mail a Friend</a></td>


    what php code do i need to scrap the last two columns (i.e., the javascript link and the email link)?


    Also, at the moment my script is just echoing the stuff onto a webpage. However, the data in each feild in columns A and B i need to link to places on my site. How can i do this? Would i be right in assuming that i have to save the two fields to mysql first to enable me to add links to them?

    if so, how do i get these two columns into two coressponding columns in mysql?

    thankyou people!

  5. #5
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74

    re

    thanks for the reply. I have managed to cut the content recived down to what i need plus 2 tables that i don't need:

    |A|B|C|D|

    A,B,C and D represent 4 columns, and belows is a sample of the data for them (i need A and B, i don't need C and D);

    <td>A5 AAB</td><td>Auction</td><td><a href="javascript: showPlate('A5%20AAB'); return true;" onclick="return showPlate('A5%20AAB');" target="dvlaplate">View Plate</a></td><td><a href="emailafriend.php?plate=A5%20AAB">E-mail a Friend</a></td></tr><tr><td>A12 AAU</td><td>250</td><td><a href="javascript: showPlate('A12%20AAU'); return true;" onclick="return showPlate('A12%20AAU');" target="dvlaplate">View Plate</a></td><td><a href="emailafriend.php?plate=A12%20AAU"> E-mail a Friend</a></td></tr>.................

    .............<tr><td>A18 ACU</td><td>250</td><td><a href="javascript: showPlate('A18%20ACU'); return true;" onclick="return showPlate('A18%20ACU');" target="dvlaplate">View Plate</a></td><td><a href="emailafriend.php?plate=A18%20ACU"> E-mail a Friend</a></td>


    what php code do i need to scrap the last two columns (i.e., the javascript link and the email link)?


    Also, at the moment my script is just echoing the stuff onto a webpage. However, the data in each feild in columns A and B i need to link to places on my site. How can i do this? Would i be right in assuming that i have to save the two fields to mysql first to enable me to add links to them?

    if so, how do i get these two columns into two coressponding columns in mysql?

    thankyou people!

  6. #6
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    To answer your first question, I would run the result of the table you want to keep through same function as before (changing the pattern you're matching, of course). Then you'll get a new array containing each row of data. Just echo the first two and ignore the rest.

    Your pattern would be /<tr[^>)>[^(<\/td>)]*<\/td>/i

    You'll lose your <table> and </table> tags with this, so you'll have to add those into your script.

    You can add data to the tables using regular expressions, too, on the fly, without going to the database. Give me an example of what the result would look like and I'll see if I can craft the expression you need. I don't know how much of a programmer you are, but you may want to look into a book on regular expressions for you want to do a lot of text manipulation like this. Reg Exps are pretty basic and very powerful, but they are rather abstract and can be hard to follow if you're new to them. I don't have it, but I know O'Riley publishes at least one very good, very comprehensive book on regexps.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  7. #7
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    Originally posted by jason
    4. preg_match stops after the first match is found, so $regs[1] would show the contents of the first table, but $regs[2] would not show the contents of the second.
    AFAIK that goes for the match counter only. (Should check my implementations to see if this is true or I was plain lucky.)

    From the manual:
    If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
    preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match.
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

  8. #8
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
    What this means is that text inside of parens will be included in the $matches array. For example, given the following expression:

    /<table([^>]*)>([^(<\/table>)]*)<\/table>/i

    The matched text from that expression would be contained in each of the elements of $matches as listed below:

    $matches[0] would contain the entire table from <table> to </table>.

    $matches[1] would contain the attributes of the table tag (border="1" width="50%"...)

    $matches[2] would contain the everything inside the table tags, but not the table tags themseleves.

    $matches[3] may either be empty (because it doesn't actually match anything), or it may contain the static text </table>. I'm not sure.

    I use this technique in a template parsing strategy. The expression looks something like /\{([^\}]*)\}/. In this case $matches[0] matches the full expression that will be substituted with text in the final output and $matches[1] contains the variable. {lastname} => $vars['lastname']....

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  9. #9
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74

    re

    i have been programming for about a month so am not really upto scratch, is that html above?!

    I have worked out a script to get what i want in php:


    $ss = "http://www.site.....co.uk/search/search.php?searched=yes&search=";
    $GrabURL = $s.$s1.$s2.$s3.$s4.$s5;
    $GrabStart = "</td><td>";
    $GrabEnd = "E-mail a Friend</a></td></tr><tr><td colspan=\"4\"><center><img src=\"../images/dotred.gif\" width=\"288\" height=\"3\">";

    $OpenFile = fopen("$GrabURL", "r"); //- DO NOT CHANGE
    $RetrieveFile = fread($OpenFile, 200000); //- Reduce This To Save Memory
    $GrabData = eregi("$GrabStart(.*)$GrabEnd", $RetrieveFile, $DataPrint);
    // $DataPrint[1] = str_replace("", "", $DataPrint[1]); //- Un-Comment This Line for "Replace" purposes!
    // $DataPrint[1] = str_replace("", "", $DataPrint[1]); //- Un-Comment This Line for "Replace" purposes!
    fclose($OpenFile); //- DO NOT CHANGE

    $DataPrint[1] = strip_tags($DataPrint[1],"");
    $DataPrint[1] = ereg_replace("View Plate","",$DataPrint[1]);
    $DataPrint[1] = ereg_replace("", "",$DataPrint[1]);

    this is part of it anyway. the variables are passed froma form where the users select what they are. however, i need to display the result, i.e. the variables, on the same page that the form is on.

    --QUESTION--
    how (or is there anyway) can you get the variable from a html form to remain on the same page as the html form?

  10. #10
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884

    Re: re

    Originally posted by davidstummer
    i have been programming for about a month so am not really upto scratch, is that html above?!
    Its PHP code to manipulate HTML that you pull from another site.

    I have worked out a script to get what i want in php:
    ...
    this is part of it anyway. the variables are passed froma form where the users select what they are. however, i need to display the result, i.e. the variables, on the same page that the form is on.
    A couple notes about your code. I noticed you used fopen() to open the contents of $GrabURL, which contained a concatenated string $s.$1.$2.$3.$s4.$5, yet I didn't see any walues for these vars, so it looks like you are passing an empty string to fopen(). I may be wrong (I'm not seeing all of your code), but it looks to me like you should be using fopen($ss, 'r');. Also, you should probably change the ereg/eregi functions to use their PCRE (Perl Compatible Regular Expressions equivalents. The PCRE functions are newer to PHP and are generally faster and a little more powerful than their ereg cousins. They aren't direct drop-in replacements however. I'd worry about getting the code working with ereg and then consider changing to PCRE (the PCRE functions start with preg_ if you're looking for them in the manual) later.

    --QUESTION--
    how (or is there anyway) can you get the variable from a html form to remain on the same page as the html form?
    I'm not sure if this is what you're asking, but you can submit your form to itself. Just set your form's action to the name of your php script (or, for optimal flexibility, set it to "<? echo($PHP_SELF); ?>"). Then, at the top of your script, put in a check to see if the form has been submitted (the easiest way is to check for a required value in one of your form fields). If its there, send the user to the final output, if not, send them to the form.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  11. #11
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74
    thanks ill try this (variable). the variables in my script are passed on from a form, so this goes to the right url, and the user gets the info they desire. my script works really well (i shouldnt take full credit as i found and modified it). it works well because the data that is collected is a few bytes.

    the reason why i needed the variable to be kept in the same file as the form, is that the script above is in the same file as the form, just alittle further down.

    one more question. how can i send the variables in this file (ie from the form) to another file, without using cookies?
    thankyou

  12. #12
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    You pass data to files one of two ways--via standard input (which is what you're doing when you set method="POST" in a form) and through the query string (method="GET" in a form). If you are passing data that isn't via a form submission, you have to use the query string method.

    The query string is attached to a URL with a question mark. It is a simple key=value listing, each key and value pair is separated with an &.

    Example:

    http://www.somedomain.com/whoami.php?firstname=Jason&lastname=Pito niak

    Spaces are represented with the + sign:
    ?fullname=Jason+Pitoniak

    Finally, if you are passing anything beyond simple alpha-numerics, you should first pass your data through the PHP function urlencode() to format it properly.

    In your receiving script (if its PHP), you can get at the query string variables by using $_GET[], $_REQUEST[], $HTTP_GET_VARS[], or simply $passedvarname.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  13. #13
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74

    re

    what if wanted to pass variables on to the nest page through the url, ie, if i already have the variables $s and $s2 like the ones above, is this just a simple case of

    http://www.somedomain.com/whoami.php?firstname=$s&lastname=$s2


    ??
    cheers

  14. #14
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    Do it either of these ways:

    1. ) echo("<a href=\"http://www.somedomain.com/whoami.php?firstname=$s&lastname=$s2\">" );

    2.) (Stating outside of the <?php ... ?> tags)
    <A href="http://www.somedomain.com/whoami.php?firstname=<?=$s?>&lastname=<? =$s2?>">

    The first example is for when you are working in PHP code and want to write a string to the browser. The second example uses a shortcut, <?=$var?>, which lets you quickly substitute a value when you're working in the pages HTML.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  15. #15
    Loyal Client
    Join Date
    Mar 2003
    Posts
    74

    re

    ta very much

    im getting the impression (i dot know why) that javascript is a lot more flexible than php when it comes to things like passing info around a site.

    would i be right? is so i think it's time to make my 'javascript for dummies' purchase, to go along with the rest of the yellow books on my shelf!

    one more question---------------------------------

    in my coe i have

    <?echo '&nbsp&nbsp' . $s1.$s2.$sp.$s3.$s4.$s5 . '&nbsp&nbsp';?>

    which outputs text onto the page. however this text is in scanky default font. how do i change this. the variables are from a form.
    Last edited by davidstummer; 06-27-2003 at 04:12 AM.

Page 1 of 2 1 2 LastLast

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
  •