Handling trailing spaces after removing words from a string.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mustikos
    New Member
    • Oct 2007
    • 3

    Handling trailing spaces after removing words from a string.

    I'm trying to figure out why, when I remove a word from a string in a text box and then split or explode the string, I'm getting a blank in the array. For example, I input, "replica wooden box" I str_replace replica and then remove and double space , I've tried several different methods and even tho the string looks fine, after reprinting it in another text box, I'm still getting the first array field empty.
  • Mustikos
    New Member
    • Oct 2007
    • 3

    #2
    Originally posted by Mustikos
    I'm trying to figure out why, when I remove a word from a string in a text box and then split or explode the string, I'm getting a blank in the array. For example, I input, "replica wooden box" I str_replace replica and then remove and double space , I've tried several different methods and even tho the string looks fine, after reprinting it in another text box, I'm still getting the first array field empty.

    update: I used trim() and am getting there. I'm missing something here, but seems it should be easier to remove two or three spaces if a person happens to enter them into a text box, say they cut and paste something and instead of
    Code:
    "replica wooden chairs"
    , they put
    Code:
    "replica    wooden chairs"
    Ok, I'm more confused now. Do I use a str_replace for 'two spaces' an 'three spaces' ?
    Last edited by pbmods; Oct 26 '07, 12:18 PM. Reason: Added CODE tags.

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Post the coding here please! Then we can take a look at on this closely. Thanks.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Mustikos. Welcome to TSDN!

        Changed thread title to better describe the problem (threads whose titles do not follow the Posting Guidelines actually get fewer responses).

        It sounds like you'll want to use preg_replace() to fix your spaces after you're done:
        [code=php]
        $str = preg_replace('/(\\s){2,}/', '$1', $str);
        [/code]

        Comment

        • Mustikos
          New Member
          • Oct 2007
          • 3

          #5
          Sorry for the delays in getting back to everyone that offered assistance... and thanks for your help; I'm not sure where I was at, but the problem solved.. couldn't say what was causing it at this time, but I'll give some details..

          First of all, I have a form with a text box as follows:

          <input name="searchtxt " size="30" value="<?php print $searchtxt; ?>">

          Then, I go like:

          $srchtxt = $_POST['$searchtxt'];

          Then, like:

          $srchtxt = str_replace("an tiques", " ", $srchtxt);

          Then, I'll do a:

          $srchtxt = str_replace(" ", " ", $srchtxt);

          $searchtxt = preg_replace('/\s\s+/', ' ', $searchtxt);... I think pbmods mentioned using that and I did and it worked. I forget, actually..

          but if I entererd 'antique desk chair' it would take out antique and I'd assign the words to an array and end up with the first one being a blank, or space...

          Anyway folks, I do appreciate your help and look forward in participating in your community, in an effort to improve all of our efforts..

          Happy New Year!

          Dave W
          aka Mustikos

          Comment

          Working...