Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    498
    Thanks
    101
    Thanked 17 Times in 17 Posts

    How to get php search to ignore image tags and code when displaying results

    I'm still working on my dictionary project. I've run into something with the search results. The words used within my image code are picked up and brought forward as a result. Just a test with no input text or caption on the image gives trouble.

    Code:
    <figure class="image"><a title="Click for larger view" href="https://www.codingforums.com/images/apt.jpg" target="_blank" rel="noopener"><img src="http://images//.jpg" alt="apartment" width="80" height="80" /></a>
    
    <figcaption>Street View</figcaption>
    
    </figure>
    For example, if I search the word "click," all entries with photos that have a hover title such as "Click for larger view" are displayed as a result. Not only that, but target, blank, height, width, etc. -- all of these words in my above code sample are potential search results.

    I don't know much about php, but I'm learning, and what I have has been obtained by paying for, piecemeal, until it has finally become something really useful. I'm down to this one hurdle that's throwing a wrench into the gears.

    Is it possible to get the php script to ignore image code -- except for captions -- when returning the results?

    Any help or ideas would be greatly appreciated.

    Thanks,
    Kevin
    Last edited by Kevin_M_Schafer; Jan 20th, 2019 at 02:12 AM. Reason: punctuation
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well. | Maintaining the aspect ratio of an image is important.

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,360
    Thanks
    10
    Thanked 1,189 Times in 1,179 Posts
    So you are searching through HTML web pages?
    Can you describe what you are searching, and the reason for it?
    Typically, you use a database to store information, images, filenames, etc. and you search (or query) the database.
    I'm not sure why someone would search the HTML static pages. Maybe you should not be using static pages but instead, render your pages dynamically by using a database?

    An example:
    You have 1000 images.
    You save all of the images in a directory.
    In a database you have a row for each image and the row columns might be ID | filename | caption | short desc | long desc |
    So now you have 1000 rows in the database table, 1 row for each image.
    When someone searches (enters a keyword), it searches all of the short and long descriptions for anything containing that word.
    Those queried results are then displayed on a web page (the query results also have the path/filename for each image).
    You could even have a column for common mis-spellings.
    Last edited by mlseim; Jan 20th, 2019 at 02:59 PM.

  3. #3
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    498
    Thanks
    101
    Thanked 17 Times in 17 Posts
    Hi mlseim,

    Thank you for the reply.

    I have a MySQL database with 155,000 rows and 9 columns. The content was uploaded as CSV. I want to call up a particular row, otherwise known as a word with a definition, and add an image using an URL among the existing text.

    Now, search the database for the word "larger", for example, and you'll get correct results and you'll also get a word and definition that doesn't have the word "larger" in it. Why did the search display this particular word and definition then? It's in the image title tag, as in "Click for larger view."

    Can the PHP script somehow ignore the image tags? How does WordPress do it? I can perform a search on a WP blog and it will find a particular word within an image caption, which is ideal, but the search ignores words that are in the image tags. How does TinyMCE and CKEditor display the rich text and show only the image? Where are the image tags when viewing the rich text display? They can certainly be viewed when looking at the source mode of the editor, but they disappear when you toggle back to rich text.

    Kevin
    Last edited by Kevin_M_Schafer; Jan 20th, 2019 at 08:11 PM. Reason: punctuation
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well. | Maintaining the aspect ratio of an image is important.

  4. #4
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    498
    Thanks
    101
    Thanked 17 Times in 17 Posts
    Maybe this screenshot will help show what is happening. I spliced two screenshots together and stacked them.

    -screenshot_of_good_-_bad_search-jpg

    A downloadable, more legible version of the same image:
    https://ufile.io/hbw8i
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well. | Maintaining the aspect ratio of an image is important.

  5. #5
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    498
    Thanks
    101
    Thanked 17 Times in 17 Posts
    I take it back. I don't think WP can search without finding the "hidden" words used within image tags. I just tried it and searches for words like "width" and "height" and "image" all returned blog posts that did not visibly use these words, but are used to display the images.

    For Pete's sake. This doesn't make sense. Potentially, someone could have a big problem on their hands. You could search the word "caption" and instead of a half dozen results, you could end up with over 100,000 results on that one word -- all bologna.

    There has to be another way to either store the entries, call the entries, or display the results.

    Kevin
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well. | Maintaining the aspect ratio of an image is important.

  6. #6
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,360
    Thanks
    10
    Thanked 1,189 Times in 1,179 Posts
    I see. So within the text of your MySQL tables, you have HTML tags mixed in with the text content?
    If that's the case, show us the query you use to search your SQL table.

    I'm thinking that the stripping out of HTML tags will happen after the query and before the words are highlighted.
    Show us some PHP scripting that does the query and makes the result words yellow.

  7. #7
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    498
    Thanks
    101
    Thanked 17 Times in 17 Posts
    Quote Originally Posted by mlseim View Post
    I see. So within the text of your MySQL tables, you have HTML tags mixed in with the text content?
    If that's the case, show us the query you use to search your SQL table.

    I'm thinking that the stripping out of HTML tags will happen after the query and before the words are highlighted.
    Show us some PHP scripting that does the query and makes the result words yellow.

    mlseim, is this what you want to see? Thanks, Kevin.

    PHP Code:
    // Get search keyword
        
    $searchKeyword = !empty($_GET['sq'])?$_GET['sq']:'';
        
    $searchStr = !empty($searchKeyword)?'?sq='.$searchKeyword:'';
        
        
    $searchFieldArr = array('word');
        if(!empty(
    $_GET['sfield'])){
            
    $searchFieldArr $_GET['sfield'];
            
    $hltdef in_array('definition'$searchFieldArr)?1:0;
        }
        if(!empty(
    $_GET['ematch']) && ($_GET['ematch'] == 1)){
            
    $exactMatch 1;
        }
        
        
    // Search DB query
        
    if(!empty($searchKeyword)){
            
    $searchArr = array();
            foreach(
    $searchFieldArr as $key=>$val){
                
    $searchArr[$val] = addslashes($searchKeyword);
            }
        }
        
    // Get count of the words
        
    $con = array(
            
    'like_or' => $searchArr,
            
    'exact_match' => $exactMatch,
            
    'return_type' => 'count'
        
    );
        
    $rowCount $db->getRows('content'$con);
        
    // Initialize pagination class
    $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" "http://";
    $currentURL $protocol $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $baseURL preg_replace('~(\?|&)page=[^&]*~''$1'$currentURL);
    $pagConfig = array(
        
    'baseURL' => $baseURL,
        
    'totalRows' => $rowCount,
        
    'perPage' => $perPageLimit
    );
    $pagination = new Pagination($pagConfig);
        
    // Get data from words
    $con = array(
        
    'like_or' => $searchArr,
        
    'exact_match' => $exactMatch,
        
    'start' => $offset,
        
    'limit' => $perPageLimit,
        
    'order_by' => 'id DESC',
    );
    $content $db->getRows('content'$con);
    }

    function 
    highlightWords($content$word){
        
    $content preg_replace('|\b(' $word ')(?![^<]+>)|i''<span class="hlw">$1</span>'$content);
        return 
    $content;
    }
    ?> 
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well. | Maintaining the aspect ratio of an image is important.

  8. #8
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,329
    Thanks
    4
    Thanked 480 Times in 468 Posts
    https://dev.mysql.com/doc/refman/8.0/en/regexp.html

    Should do the trick. Just use an expression designed to omit results inside a tag such as:

    WHERE REGEXP_LIKE (larger)(\?![^<]*>|[^<>]*</)

    Untested, been a couple years since I did anything like that.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  9. #9
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    498
    Thanks
    101
    Thanked 17 Times in 17 Posts
    Quote Originally Posted by deathshadow View Post
    https://dev.mysql.com/doc/refman/8.0/en/regexp.html

    Should do the trick. Just use an expression designed to omit results inside a tag such as:

    WHERE REGEXP_LIKE (larger)(\?![^<]*>|[^<>]*</)

    Untested, been a couple years since I did anything like that.
    Hi deathshadow,

    Thank you for the reply.

    I admittedly don't know PHP yet. Is what you're suggesting a form of exclusion list? A list of words -- that if they're inside tags -- they would be ignored? So if I search for the word "larger", I would get its definition, but any database use of the word "larger" that is contained within a tag would be ignored? This would be great!

    I hope I understand what you're telling me.

    Kevin
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well. | Maintaining the aspect ratio of an image is important.

  10. #10
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,360
    Thanks
    10
    Thanked 1,189 Times in 1,179 Posts
    How about this ....

    PHP Code:
    $content $db->getRows('content'$con); 
    $content strip_tags($content); 
    After you get the content from the database into a string, strip out HTML tags.

    Does that work?

    EDIT:
    Oh, but wait, once you strip out HTML tags, you can't put them back in to display the original HTML content.
    darn ... not sure how to do it.
    Last edited by mlseim; Jan 21st, 2019 at 04:18 AM.

  11. #11
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    498
    Thanks
    101
    Thanked 17 Times in 17 Posts
    Thanks for the input and effort, mlseim. I appreciate it.

    Kevin
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well. | Maintaining the aspect ratio of an image is important.

  12. #12
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,360
    Thanks
    10
    Thanked 1,189 Times in 1,179 Posts
    I'm sure the part that you need to focus on is where it turns the words "yellow", or where you place the words into a span class.

    Look at PHP regex ... where you are doing preg_replace.

    There has to be a way to select all of the 'key words', but omit, or ignore, anything within certain tags.
    What that regex code looks like ... whew ... someone on this forum has to be an expert on regex.
    Hopefully they reply.

  13. #13
    Regular Coder Kevin_M_Schafer's Avatar
    Join Date
    Apr 2011
    Location
    Fairfax, Minnesota, U.S.A.
    Posts
    498
    Thanks
    101
    Thanked 17 Times in 17 Posts
    My coder said MySQL 8 would allow Expressions and it would search by that. I'm running XAMPP, and although I don't know what version my latest XAMPP download has, eventually my project would go online in my hosting account. They're BH and they're running MySQL version 5.6.

    I guess we're going to add another field to the content table and perform the task with a workaround.

    Thanks,
    Kevin
    My keyboard is an IBM from 1993 and I like it that way. | Who is Dan Well? Everyone always says I know Dan Well. | Maintaining the aspect ratio of an image is important.


 

Tags for this Thread

Posting Permissions

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