SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: explode problem

  1. #1
    Non-Member
    Join Date
    Jul 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    explode problem

    when i have a search term in a form and then do :

    PHP Code:
    $keywordssplit explode(" "$keywords);

    foreach (
    $keywordssplit as $keyword) {
    echo 
    $keyword;

    If my search term is "test" it echos test 5 times?

  2. #2
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I don't know why that would happen.

    When I run this:

    PHP Code:
      <?php
      
      $keywords 
    ' test ';
      
      
    $keywordssplit explode(" "trim($keywords));
      
      foreach (
    $keywordssplit as $keyword
      {
        echo 
    $keyword."\n";
      } 
      
      
    ?>
    I get one "test". Do a var_dump($keywords) before you explode it and see what's in it.
    (Incidentally, you should trim the string before you give it to explode so you don't get empty elements returned in the array)

  3. #3
    Non-Member
    Join Date
    Jul 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Im assuming it's a problem with the get string and it putting in some dodgy objects so ill check it out, thanks for the advice i didnt know the vardump method.

  4. #4
    Non-Member
    Join Date
    Jul 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting this from vardump :S

    string(4) "test" string(4) "test" string(4) "test" string(4) "test" string(4) "test"

    Trim doesn't solve it..

    Even tried post from the form and still it's putting four or five in the variable

  5. #5
    Non-Member
    Join Date
    Jul 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK fixed, it was inside the while loop so getting assigned 5 times due to 5 records, oops

    Now the next problem, I'm trying to highlight keywords in the search results:

    PHP Code:
    /* do replacing */
    foreach ($keywordssplit as $keyword) {
    $description preg_replace "'($keyword)'si" "<font color=\"red\">\\1</font>" $row["description"]);
    }
    foreach (
    $keywordssplit as $keyword) {
    $title preg_replace "'($keyword)'si" "<font color=\"red\">\\1</font>" $row["title"]);

    $keywordssplit var_dump for the search terms test manager is
    Code:
    array(2) { [0]=>  string(4) "test" [1]=>  string(7) "manager"
    for some reason it only highights "manager" but not "test" in the results, any ideas?

  6. #6
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Cavalli
    $keywordssplit var_dump for the search terms test manager is
    Code:
    array(2) { [0]=> string(4) "test" [1]=> string(7) "manager"
    for some reason it only highights "manager" but not "test" in the results, any ideas?
    If you're using $description after the loop then you're only getting what the last assignment of $description is. In other words, $description gets a totally new value with each loop iteration, whatever was in the last iteration is lost.

    Try this:

    PHP Code:
     
     $description 
    $row['description'];
     
     foreach (
    $keywordssplit as $keyword) {
       
    $description 
         
    preg_replace "'($keyword)'si" "<font color=\"red\">\\1</font>" $description);
     } 

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    simple testing: do echo or print_r (depending on if it is array or string) before and after each action to see where it changes wrongly and then search for problem

  8. #8
    Non-Member
    Join Date
    Jul 2005
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it, thanks lads.

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
  •