1

I'm trying to remove spaces from string but only from beggining and end of the string. Everythink work fine but after insert string to database there is no spaces but in the same place is "�".

Here is my code:

$synonimy = $_POST['fsynoms'];
$synonimyPodzielone = explode( ',', $synonimy );
for($k=0; $k < count($synonimyPodzielone); $k++)
{
    for($m=0; $m<strlen($synonimyPodzielone[$k]);$m++)
    {

        if($synonimyPodzielone[$k][$m]==" ")
        {
            $synonimyPodzielone[$k][$m]="";
        }
        else
        {
            break;
        }
    }
}

I have also tryied with str_replace trim and same problem.

Any idea how to do that?

3
  • 2
    use PHP Trim function? Commented Jan 27, 2015 at 11:02
  • � looks like an type encoding problem.. Commented Jan 27, 2015 at 11:14
  • Yes it could be but before removing spaces it works perfect Commented Jan 27, 2015 at 11:16

5 Answers 5

1

What you're looking for is trim. With the help of array_map you can even skip the for loop.

$input = " foo , bar , baz , qux ";
$words = explode(",", $input);
$wordsTrimmed = array_map("trim", $words);
$csvString = implode(",", $wordsTrimmed);

echo $csvString; // foo,bar,baz,qux

You can of course make it neater:

function trimWords($string, $glue = ",") {
    return implode($glue, array_map("trim", explode($glue, $string)));
}

$input = " foo , bar , baz , qux ";
$csvString = trimWords($input);

echo $csvString; // foo,bar,baz,qux
Sign up to request clarification or add additional context in comments.

2 Comments

yes but what if there is $input = " foo , bar two , baz , qux" and i want to keep word "bar two" no bartwo
@krystian Trim only removes leading and trailing whitespace.
1

The trim($str) function ( http://php.net/manual/en/function.trim.php ) is hard to use incorrectly, were you just not storing the return value? (the trim function in PHP does not modify the argument, it instead returns a new string with whitespace removed).

$synonimy = $_POST['fsynoms'];
$synonimy = trim( $synonimy );

insert_into_database( $synonimy );

1 Comment

yes but if i have an array with strings like: $tab=['word1', ' word2 ', word-part1 word-part2 ']; i want to remove spaces only before word and after word no between word-part1 and word-part2;
1

PHP Trim: http://php.net/manual/en/function.trim.php should make life much easier.

for($m=0; $m<strlen($synonimyPodzielone[$k]);$m++)
{
    $synonimyPodzielone[$k][$m] = trim($synonimyPodzielone[$k][$m]);    
}

1 Comment

yes i have tryied it and it work but after insert string to database in place of spaces are "��". Example i want to put word : Hello but before Hello are two spaces i want to remove it and use trim. After that there is ��
0

Or even simpler.

$synonimy = trim($_POST['fsynoms']);
insert_into_database($synonimy);

Comments

0

using strtr is also possible (and fast):

$synonimy = trim($_POST['fsynoms']);
$synonimy = strtr($synonimy, array(' ,' => ',', ', '=>','));

Note: if your string contains duplicate spaces you want to remove, in this case you can use this:

$synonimy = preg_replace('~\s*(,)\s*|\A\s+|\s+\z~u', '$1', $_POST['fsynoms']);

that deals with unicode strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.