0

This is driving me nuts. I've searched for solutions but can't figure out what's wrong.

The situation... I have an SQL query running in WordPress, as follows:

SELECT distinct guid
FROM $wpdb->posts
WHERE post_status = 'inherit'
AND guid is not null

Dead simple. It returns a single column which contains a list of all the attachment media files currently stored on the system (well, not all of them, but it'll do as an explanation).

In a WP plugin function, I run the query:

$media_library_files = $wpdb->get_col($get_all_media,0);

That returns an array (I don't want an object) with values like:

[0] => http://mysitename.com/wp-content/uploads/2013/05/thumb_littlefile_blah.jpg

Then I want to process each one so that there's just the filename left. The problem is that, when I run a str_replace or pretty much any other string function on the contents, it doesn't work. For example:

$horrid_bit = 'http://mysitename.com/wp-content/uploads/2013/05/';
foreach($media_library_files as $item) {
    $item = str_replace($horrid_bit,'',$item);
}

When I print_r the array after that, there's no visible change - every line is exactly the same as it was before.

I've tried using a (string) to cast $item, I've tried defining variables to do that, then working the str_replace on them, I've tried... loads of different things.

I have a feeling I'm missing something really simple, but I just can't see it. Is it because the column is varchar in the original table? Or something else?

Any help appreciated. Thanks!

Spike --
  • 23
  • 1
  • 7
  • Have a look at [the `foreach` docs](http://us1.php.net/manual/en/control-structures.foreach.php) (the first listed example) You need to iterate over it with `&$item` as a reference to be able to modify the array in place. – Michael Berkowski Nov 15 '13 at 14:16
  • 1
    I also recommend [`basename()`](http://us1.php.net/manual/en/function.basename.php) instead of `str_replace()` to get just the filename part. – Michael Berkowski Nov 15 '13 at 14:16
  • Ahhhh, so simple. I knew I was being daft. I didn't think basename would work on URLs, but it apparently does, so a double thank-you for that! – Spike -- Nov 15 '13 at 14:39

4 Answers4

1

PHP is not my 'native languague', but it seems like you're not modifying the values in the array. Did you try to put the modified string items (=minus the path) in a new array and use that one?

$horrid_bit = 'http://mysitename.com/wp-content/uploads/2013/05/';
$new_array = array();
foreach($media_library_files as $item) {
    $new_item = str_replace($horrid_bit,'',$item);
    $new_array.push($new_item);
}
//...use the items in the $new_array 

Also, you might wanna just read the whole path string as an array (split on '/')and take the last element to get to the file.

Lave Loos
  • 1,252
  • 1
  • 11
  • 15
  • I tried that and it doesn't work, as $item is not a string unless it's &$item (as Michael noted above). :( – Spike -- Nov 15 '13 at 18:52
1
$horrid_bit = 'http://mysitename.com/wp-content/uploads/2013/05/';
foreach($media_library_files as $key => $value) {
       $media_library_files[$key] = str_replace($horrid_bit,'', $value);
    }

But as Michael pointed out if you are not doing it this way for a specific purpose using basename would be better here, so you don't have to worry about the folder changing each month/year.

foreach($media_library_files as $key => $value) {
       $media_library_files[$key] = basename($value);
    }
Tom Tom
  • 3,680
  • 5
  • 35
  • 40
0

You are not actually changing the value of $item in your array, you are instead creating a new variable, which is then overwritten every time your loop iterates.

It's also a good idea to check that the array is not empty before attempting a foreach() loop on it.

Finally, I've replaced str_replace() with basename() (as suggested in the comments under your question by @MichaelBerkowski).

if(!empty($media_library_files)) : foreach($media_library_files as $item) :
        $media_library_files[$item] = basename($item);
    endforeach;
endif;
David Gard
  • 11,225
  • 36
  • 115
  • 227
0

str_replace() can handle arrays. No need for your foreach loop.

$horrid_bit = 'http://mysitename.com/wp-content/uploads/2013/05/';
$media_library_files = str_replace( $horrid_bit,'',$media_library_files );

On the other hand, since it seems you want to remove the path and get only the filename:

foreach($media_library_files as $key => $item) {
  $media_library_files[$key] = basename($item);
}

This will change all entries to the filename only. The way you have it now you will have to change the $horrid_bit every month since the uploads are organized in year/month/ folders.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78