0

I want to replace dashes & underscores with space in this image file name:

text1_text2-10212-etc_125.jpg (result of a plugin)

My code is: $string = preg_replace('/[\-_]/',' ', $file['name']);

I'm trying with str_replace & preg_replace but it doesn't work. This code is part of the Advanced Image Grabber plugin ( in class_image_grabber.php)

//takes an image url as argument and save is to media library
// return Null on failure
static public function imageUpload($imageUrl) {
    $file = array();
    $file['name'] = basename($imageUrl);
    $file['tmp_name'] = download_url($imageUrl);
    if (is_wp_error($file['tmp_name'])):
    @unlink($file['tmp_name']);
    return NULL;
    endif;
    $attachmentId = media_handle_sideload($file, 0);
    // create the thumbnails
    $attach_data = wp_generate_attachment_metadata( $attachmentId,  get_attached_file($attachmentId));
    wp_update_attachment_metadata( $attachmentId,  $attach_data );
    return $attachmentId;   
}//EOF
3
  • $string = str_replace( array( '-', '_' ), ' ' , $file['name'] );. But remember that these changes will disappear after plugin update. – Max Yudin Jul 1 '18 at 14:33
  • did'nt work. the result is text1-text2-10212-etc_125.jpg. if I remove a space the result is : text1text210212etc125.jpg . 2 years is not updated. maybe the author forgot this plugin: D – Aube Jul 1 '18 at 15:55
  • What does echo $file['name']; prints out? – Max Yudin Jul 2 '18 at 8:07
-1
$file_name = "text1_text2-10212-etc_125.jpg";
$result = preg_replace("/[\-_]/", " ", $file_name);
ECHO $result;

OUTPUT

text1 text2 10212 etc 125.jpg

Link for DEMO

1
  • The question already includes that line of code. They already know that part. The part they're asking about is doing it in the context of an image uploaded by a plugin. This answer does not address the actual question. – Jacob Peattie Jul 3 '18 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.