Sostituire trattino (-) e underscore (_) con spazio
1 lug 2018, 17:21:30
Visualizzazioni: 19.6K
Voti: 0
Voglio sostituire trattini e underscore con spazi nel nome di questo file immagine:
text1_text2-10212-etc_125.jpg (risultato di un plugin)
Il mio codice è: $string = preg_replace('/[\-_]/',' ', $file['name']);
Sto provando con str_replace
e preg_replace
ma non funziona. Questo codice fa parte del plugin Advanced Image Grabber (in class_image_grabber.php
)
//prende un URL immagine come argomento e la salva nella libreria media
// restituisce Null in caso di fallimento
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);
// crea le miniature
$attach_data = wp_generate_attachment_metadata( $attachmentId, get_attached_file($attachmentId));
wp_update_attachment_metadata( $attachmentId, $attach_data );
return $attachmentId;
}//EOF
Commenti
Tutte le risposte alla domanda
1
1
$file_name = "text1_text2-10212-etc_125.jpg";
$result = preg_replace("/[\-_]/", " ", $file_name);
ECHO $result;
RISULTATO
text1 text2 10212 etc 125.jpg

Linherest
3
3 lug 2018 12:15:18
Domande correlate
2
risposte
3
risposte