3

I'm developing a word counting application and I'm using following code to load words to an array.

$str = ' Some string is here     ...  Another string.. ';
$str = trim($str);
$str = preg_replace('/\s+/', ' ', $str);
$str_array = explode(" ", $str);
$str_array = array_filter($str_array);

But the issue is I'm getting empty values in my array like follows.

Array
(
    [0] => Word1
    [1] => Word2
    [2] => Word3
    [3] => 
    [4] => 
    [5] => 
)

I've tried array_filter($str_array) and more stackoverflow answers. But I was failed to get this done. Can some one please help me to fix this issue? Thanks!

7
  • 1
    Well, I'm not going to answer the actual question, but you can change your regex to (\w+) to extract words. It'd be more efficient. Commented Mar 3, 2014 at 5:25
  • 2
    @OP: Testing out the string you have provided works fine here, you should either be able to reproduce this problem or give the exact string you are having trouble with. Commented Mar 3, 2014 at 5:28
  • This is the actual string They provide a wide range of services that will help you with your Internet marketing concerns. Commented Mar 3, 2014 at 5:35
  • 1
    @OP: This is fine too. What's really the problem? Commented Mar 3, 2014 at 5:39
  • I'm phrasing a word document actually. $str contains phrased string. With var_dump, these empty values shows as string '' (length=1) Commented Mar 3, 2014 at 5:46

4 Answers 4

7

Trim the array elements using trim by array_map and finally do an array_filter using strlen as the call-back.

<?php
$arr=Array
(
    0 => 'Word1',
    1 => 'Word2',
    2 => 'Word3',
    3 => ' ',
    4 => '',
    5 => ''
);

$new_arr = array_filter(array_map('trim',$arr),'strlen');
print_r($new_arr);

Demo

Sign up to request clarification or add additional context in comments.

5 Comments

Can you var_export your array on your question before array_filter ? See the demo
using print_r(array_filter($str_array));
got 531 => '', 532 => '', 533 => '',
@madhushankarox, I had asked you to the update the var_export value of your array on the question without its hard to see what is inside your array.
Please see this >> s29.postimg.org/ju987r6ef/Capture.png that string contains some special characters
1

Try this...

 foreach ($array as $key=>$value) 
 { 
     if($value == '')
     {
       unset($array[$key]);
     }
 }

8 Comments

It's actually a word document. I'm phrasing a .doc and loading contents. But when I use var_export, these empty values returns as 531 => '', 532 => '', 533 => ''
Here is the text They provide a wide range of services that will help you with your Internet marketing concerns.
its strange.. when I directly pass this string.. it doesnt give any blank value in the array..
try printing the array using var_dump and check if it returns length for that blank values...
My variable contains that string.
|
0
$str = ' Some string is here     ...  Another string.. ';
$str = trim($str);
$str = str_replace('  ', ' ', $str); //replace two space to single space
$str_array = explode(" ", $str);

Comments

0

I was able to fix this issue with $str = preg_replace('/[^(\x20-\x7F)]*/','', $str);

Source

        $str = strip_tags($str);
        $str = trim($str);
        $str = preg_replace('/ HYPERLINK "[^"]*" /', '', $str);
        $str = preg_replace('/\s+/', ' ', $str);
        $str = preg_replace('/[^(\x20-\x7F)]*/','', $str);

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.