PHP:preg_replace& mb_strtoupper不会一起工作

时间:2017-08-07 23:14:30

标签: php

我尝试从我的String中删除[Brackets]的所有内容,并使用mb_strtoupper。但它不起作用。

我想得到:

<artist> <![CDATA[ MALARKEY FT. STEVYN ]]> </artist> <title> <![CDATA[ TO YOU ]]> </title>

但我明白了:

<artist> <![CDATA[ MALARKEY FT. STEVYN ]]> </artist> <title> <![CDATA[ TO YOU [DOMASTIC REMIX] ]]> </title>

这是我的代码:

$Length = 25;
$rnd = substr(str_shuffle(md5(time())), 0, $Length);

$songs = [];

$channels = range(1,6);

// Download current songs (from 1 to 6)
foreach ($channels as $sid) {

    $song = file_get_contents(sprintf('http://31.214.240.175:8000/currentsong?sid=%s', $sid));
    list($artist, $title) = explode(' - ', $song, 2);

    $actualImageUrl = sprintf('http://31.214.240.175:8000/playingart?sid=%s&rnd=%s', $sid, $rnd);
    $placeholderImageUrl = "http://www.reyfm.de/img/nocover.png";
    $imageUrl = @getimagesize($actualImageUrl) ? $actualImageUrl : $placeholderImageUrl;

    $songs[] = [
        'rnd' => $rnd,
        'sid' => $sid,
        'image' => $imageUrl,
        'song' => $song,    

        'artist' => trim($artist),
        'title' => trim($title),

        'artist' => preg_replace('~\[.*\]~' , "", $artist),
        'title' => preg_replace('~\[.*\]~' , "", $title),

        'artist' => mb_strtoupper($artist),
        'title' => mb_strtoupper($title),
    ];
}
unset($song);

一些想法?

1 个答案:

答案 0 :(得分:0)

您正在创建一个数组,并覆盖'artist''title'两次。

$songs[] = [
    'rnd' => $rnd,
    'sid' => $sid,
    'image' => $imageUrl,
    'song' => $song,    

    'artist' => trim($artist),
    'title' => trim($title),

    'artist' => preg_replace('~\[.*\]~' , "", $artist),
    'title' => preg_replace('~\[.*\]~' , "", $title),

    'artist' => mb_strtoupper($artist),
    'title' => mb_strtoupper($title),
];

这与写作几乎相同:

$songs[0]['artist'] = trim($artist);
$songs[0]['artist'] = preg_replace('~\[.*\]~' , "", $artist);
$songs[0]['artist'] = mb_strtoupper($artist);

如您所见,$songs[0]['artist']的值以最后一个值结束。你想要的是这样的:

$artist = trim($artist);
$artist = preg_replace('~\[.*\]~' , "", $artist);
$artist = mb_strtoupper($artist);

// Similar code for $title

$songs[] = [
    'rnd' => $rnd,
    'sid' => $sid,
    'image' => $imageUrl,
    'song' => $song,    

    'artist' => $artist,
    'title' => $title,
];