Jump to content

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
https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/
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>';
?>

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..

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.

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);
?>

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.