473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Oct 26 '07 #1
4 2996
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
Expand|Select|Wrap|Line Numbers
  1. "replica wooden chairs"
, they put
Expand|Select|Wrap|Line Numbers
  1. "replica    wooden chairs"
Ok, I'm more confused now. Do I use a str_replace for 'two spaces' an 'three spaces' ?
Oct 26 '07 #2
ak1dnar
1,584 Expert 1GB
Post the coding here please! Then we can take a look at on this closely. Thanks.
Oct 26 '07 #3
pbmods
5,821 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. $str = preg_replace('/(\\s){2,}/', '$1', $str);
  2.  
Oct 26 '07 #4
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("antiques", " ", $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
Jan 15 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely....
1
by: Andy Visniewski | last post by:
I have three columns, RecordID, FirstName, and LastName, but somehow through some program glitch, there is sometimes a trailing space in the firstname and lastname columns, for example, a persons...
4
by: ucfcpegirl06 | last post by:
Hi, I need help getting rid of trailing white spaces. I am searching a file for various data (not important) and retrieving it. I output the data if found to a file. An example would be:...
0
by: Seth | last post by:
First off, my apologies if this is in the wrong newsgroup, but I hope I'm close enough. I'm trying to do some parsing of a CSV file using OleDbConnection, but for some reason, when I populate my...
17
by: tommy | last post by:
Hi all, I' m adding strings to some fields in my table via Access. The strings sometimes have trailing spaces and I really need to have it that way, but Access truncates trailing spaces. How can...
3
by: Paul | last post by:
Hi, My RichTextBox has multiple lines of text. Most of the lines unfortunately end with a space. Is it possible to replace the space and NewLine/Line Feed with just the NewLine/LineFeed? So...
2
code green
by: code green | last post by:
I am trying to write a simple function that will take a string containing an address line or business name and return it nicely formatted. By this I mean extra spaces removed and words capitalised....
5
by: brian.j.parker | last post by:
Hey all, I've noticed an obscure little quirk: it appears that if you use a login with trailing spaces on the name, SYSTEM_USER automatically trims those trailing spaces in SQL Server 2000, but not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.