1

I tried to Remove all content with [Brackets] from my String and also use mb_strtoupper. But it doesn't work.

I want to get:

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

But I get:

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

Here is my code:

$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);

Some ideas?

Nilaxy
  • 11
  • 2
  • do you know the purpose of those "brakets"? In other words do you know what CDATA is, typically it's used for escaping HTML and other ( things that could be seen as XML ) content within an XML document. https://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean – ArtisticPhoenix Aug 08 '17 at 00:50
  • Oh, the point is you might have better luck using something like SimpleXML to read the data with, in the first place ( if it's indeed XML ) – ArtisticPhoenix Aug 08 '17 at 00:53

1 Answers1

0

You are creating an array, and overwriting the 'artist' and 'title' keys twice.

$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),
];

This is almost the same as writing:

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

As you can see, the value of $songs[0]['artist'] ends up with the last value. What you want is something like this:

$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,
];
CJ Dennis
  • 4,226
  • 2
  • 40
  • 69