SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Parsing in PHP

  1. #1
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not entirely sure how to do this.

    This is for a search script. What I'd like to do is to not only identify which chapter certain text is in. but also parse and return say 50 characters on either side of the search term.

    So if anyone knows the functions and structure to do this (not the DB access stuff, just the parsing of the results for the surrounding words) I'd appreciate it.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  2. #2
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is how you'd do it.

    $thefinalthing = substr($TEXT_WITH_SEARCH_TERM_IN_IT, $location_of_start_of_searchterm - 50, $location_of_end_of_searchterm + 50);

    Hope this helps....

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris just came up with a perfect solution for this all int the query

    SELECT *, SUBSTR(fieldtosearch, LOCATE($searchterm, fieldtosearch) - 50, LOCATE($searchterm, fieldtosearch) + (LENGTH($searchterm) + 50)) as synopsis from tablename WHERE fieldtosearch LIKE '%$searchterm%'

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That isn't working freddy.

    Error performing query: You have an error in your SQL syntax near '(Chp_Text, LOCATE(fields, Chp_Text) - 50, LOCATE(fields, Chp_Text) + (LENGTH(fie' at line 1
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what is fields? if it is var it should be $fields if it just text that you are searching for it should be 'fields' it works I tested it.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats the results.

    fields was $term but fields was the search term.

    the sql looks like this:

    $result2 = mysql_query("SELECT Chapter_Title, Chapter_Num, Title, Book_Info.BookID SUBSTR(Content.Chp_Text, LOCATE($term, Content.Chp_Text) - 50, LOCATE($term, Content.Chp_Text) + (LENGTH($term) + 50)) as synopsis FROM Content, Book_Info WHERE Book_Info.AuthorID like '$AID' and Book_Info.BookID = Content.BookID and Content.Chp_Text like '%$term%' GROUP BY Content.BookID", $db);
    if (!$result2) { echo("<P>Error performing query: " . mysql_error() . "</P>");
    exit();


    and the above is the error I get

    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  7. #7
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    shoot I see it.... no comma before substr.. its always the stupid things

    Nope... added the comma... still get the error
    <Edited by aspen on 01-15-2001 at 11:01 PM>
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DUH my mistake it is SUBSTRING in MySQL not SUBSTR like php sorry
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought it might be that, since when using ODBC thats what the function is... but I tried that and it didn't work. Of course that is before I saw the comma... so try it now....

    Nope.. still not... but the error starts a little later in the query.

    $result2 = mysql_query("SELECT Chapter_Title, Chapter_Num, Title, Book_Info.BookID, SUBSTRING(Chp_Text, LOCATE($term, Chp_Text) - 50, LOCATE($term, Chp_Text) + (LENGTH($term) + 50)) as synopsis FROM Content, Book_Info WHERE Book_Info.AuthorID like '$AID' and Book_Info.BookID = Content.BookID and Content.Chp_Text like '%$term%' GROUP BY Content.BookID", $db);
    if (!$result2) { echo("<P>Error performing query: " . mysql_error() . "</P>");
    exit();


    Now if I search for fields I get this error:

    Your search term was found in the following:

    Error performing query: You have an error in your SQL syntax near 'fields, Chp_Text) - 50, LOCATE(fields, Chp_Text) + (LENGTH(fields) + 50)) as syn' at line 1


    If I search for anything else I get...

    Your search term was found in the following:

    Error performing query: Unknown column 'dickens' in 'field list'

    weird

    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where did dickens come from?


    Here is something I used on my own table and it worked perfect it returned a total 25 characters 10 before Blues and ten after.
    mysql> SELECT City, SUBSTRING(Description, LOCATE('Blues', Description) - 10, LOCATE('Blues', Description) + LENGTH('Blues') + 10) as synop from cities WHERE Description LIKE '%Blues%';
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay here is/was the problem.

    I needed to include single quotes around $term in the SQL.

    Now there is still a problem with your SQL though.

    the SUBSTRING function is like this

    SUBSTRING(string, start, count)

    not

    SUBSTRING(string, start, end)

    So the search field being the entire chapter text the search term could be found on position 10,124 and in your sql that'd start it at position 10,099 and end it at position 20,000 and some.

    No big deal, easy to fix.

    Thanks for all your help. I appreciate it.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dang I don't know what to say! I stand corrected I could of sworn that it was SUBSTRING(str, start, finish) but I was wrong anyways I guess we both learned something. Thanks for correcting me.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •