Jump to content

cleaning up data in an array


cyberkiller

Recommended Posts

So I have an array with 10 items in it. How do I go about clearing all spaces before the data?

 

Ex:

Before:        jacob

After: jacob

 

Also some of the data looks like this, World of Warcraft Guide

 

How do I go about checking each value for this   and replacing it with a space if its there?

 

Thanks

Link to comment
Share on other sites

<?php
$string = "World of Warcraft Guide";

$string = str_replace(' ', ' ', $string);

 

<?php
$string = '         jacob';
$string = trim($string, ' ');

 

www.php.net/trim

www.php.net/str_replace

Link to comment
Share on other sites

Putting it all together

 

<?php

$ar = array (
      '        jacob',
      'Esau',
      'World of Warcraft Guide'
    );

foreach ($ar as $k => $val) {
    $ar[$k] = trim(str_replace(' ', ' ', $val));
}

/**
* check results
*/
echo '<pre>', print_r($ar, true), '</pre>';
?>

Link to comment
Share on other sites

Doesn't seem to work for me, here is what I used

 

foreach ($parsed as $k => $val) {
    $parsed[0][$k] = trim(str_replace('&#32;', ' ', $val)); }

 

No errors, just that the values still have the spaces and &#32

 

Where the 10 values are in, $parsed[0][0], $parsed[0][1], etc..

Link to comment
Share on other sites

I did start out with barand's code, however I don't think he knew I was using a multi array (whatever their called), after playing with it,

 

foreach ($parsed[0] as $k => $val) {
$parsed[0][$k] = trim(str_replace('&#32;', ' ', $val));
}

 

Seems to work, it wasn't barand's fault he just didnt know how my array was setup.

Link to comment
Share on other sites

I did start out with barand's code, however I don't think he knew I was using a multi array (whatever their called)...

 

you could have created a recursive function:

<?php
        function stripString($array){
                foreach($array as $key => $val){
                        if(is_array($val)){ stripString($val); }
                        $array[$key] = trim(str_replace(' ', ' ', $val));
                }
        }

        $ar = array (
              '        jacob',
              'Esau',
              'World of Warcraft Guide'
            );

        stripString($ar);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.