{SOLVED} PHP trim whitespaces

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhing
    New Member
    • Oct 2006
    • 43

    #1

    {SOLVED} PHP trim whitespaces

    hiya!!!
    I have a php program that retrieve data from the database. The result is not consistent, i mean, there are many whitespaces in between characters. Like cs 143, the result will look like "cs 143", my problem is i want to have a single space in between it.. help pls..
    tnx
  • TheMadMidget
    New Member
    • Oct 2006
    • 98

    #2
    Your message appears to only have a single spacing, but hopefully this will help.
    str_replace($fi nd, $replace, $str);
    so you can use it as
    $entry = (" ", " ", $entry);

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      When you display the string in HTML it does not really matter, because the browser will remove extraneous blanks. When you want to use the data to store or other non-display function, you'll need a loop to remove all blanks except just 1. A lot easier is to use a logical expression. See the following snippet and run it, so you'll see the result.
      [php]<?php
      $data = "A B C D E F G";
      echo '<br><pre>';
      var_dump($data) ;
      // result: string(30) "A B C D E F G"
      $data = preg_replace('/(\\s+)/', ' ',$data);
      echo '<br><pre>';
      var_dump($data) ;
      // result: string(13) "A B C D E F G"
      ?>[/php]
      Ronald :cool:

      Comment

      • bhing
        New Member
        • Oct 2006
        • 43

        #4
        thanks much... ronald...
        i got my idea though your suggestions...
        actually i used ereg_replace function....
        so my code looks like this one
        ereg_replace(" +"," ",$query["courseid"]);

        GodSpedd!!!!

        Comment

        Working...