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.
Handling trailing spaces after removing words from a string.
Collapse
X
-
Originally posted by MustikosI'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"
Code:"replica wooden chairs"
-
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
-
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 MustikosComment
Comment