PHP str_replace无法正常工作,单引号

时间:2013-09-01 06:24:13

标签: php

我的问题令人沮丧,我正在从itunes的API中获取封面艺术。 这是获取歌曲的艺术家和名字并检索艺术品的代码:

$str = $song[0];
$artist_name = $song[0];
$posted = preg_replace('/ -.*/', '', $artist_name);
$manual_referer = 'http://itunes.com/';
$itunes_song = str_replace("'", '', $str);
$args = array(
    'term' => $itunes_song,
    'entity' => 'song',
    'limit' => '1',
);

echo $song_itunes;
$url = "https://itunes.apple.com/search?";
foreach ($args as $key => $val) {
    $url .= $key . '=' . rawurlencode($val) . '&';
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $manual_referer);
$body = curl_exec($ch);
curl_close($ch);

$json = json_decode($body, true);
$results = $json['results'];

foreach ($results as $result) {
    $res = $result['artworkUrl100'];
}

现在,我的问题是: 让我们说这首歌的名字是:T Pain - 5 O'Clock,因为系统有单引号歌曲的问题,我做了这两行:

$itunes_song = str_replace("'", '', $str);

'term' => $itunes_song,

但是$ itunes_song的输出是: 当需要 T Pain - 5 OClock (没有单引号)时, T Pain - 5 O'Clock

我不知道我在这里做错了什么,有人可以帮我吗? :(

完整代码:

Dachi, the full script:

<?php
include('config_radio.php');

$scfp = fsockopen("$scip", $scport, &$errno, &$errstr, 30);
if (!$scfp) {
    $scsuccs = 1;
    echo '' . $scdef . ' is Offline';
}
if ($scsuccs != 1) {
    fputs($scfp, "GET /admin.cgi?pass=$scpass&mode=viewxml HTTP/1.0\r\nUser-Agent: SHOUTcast Song Status (Mozilla Compatible)\r\n\r\n");
    while (!feof($scfp)) {
        $page .= fgets($scfp, 1000);
    }

    //define  xml elements
    $loop = array(
        "STREAMSTATUS", "BITRATE", "SERVERTITLE", "CURRENTLISTENERS",
        "MAXLISTENERS", "BITRATE"
    );
    $y = 0;
    while ($loop[$y] != '') {
        $pageed = ereg_replace(".*<$loop[$y]>", "", $page);
        $scphp = strtolower($loop[$y]);
        $$scphp = ereg_replace("</$loop[$y]>.*", "", $pageed);
        if ($loop[$y] == SERVERGENRE || $loop[$y] == SERVERTITLE || $loop[$y] == SONGTITLE || $loop[$y] == SERVERTITLE)
            $$scphp = urldecode($$scphp);

        // uncomment the next line to see all variables
        //echo'$'.$scphp.' = '.$$scphp.'<br>';
        $y++;
    }
    //end intro xml elements
    ######################################################################################################################
    ######################################################################################################################
    /////////////////////////part 2\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    //get song info and history
    $pageed = ereg_replace(".*<SONGHISTORY>", "", $page);
    $pageed = ereg_replace("<SONGHISTORY>.*", "", $pageed);
    $songatime = explode("<SONG>", $pageed);
    $r = 1;
    while ($songatime[$r] != "") {
        $t = $r - 1;
        $playedat[$t] = ereg_replace(".*<PLAYEDAT>", "", $songatime[$r]);
        $playedat[$t] = ereg_replace("</PLAYEDAT>.*", "", $playedat[$t]);
        $song[$t] = ereg_replace(".*<TITLE>", "", $songatime[$r]);
        $song[$t] = ereg_replace("</TITLE>.*", "", $song[$t]);
        $song[$t] = urldecode($song[$t]);
        $dj[$t] = ereg_replace(".*<SERVERTITLE>", "", $page);
        $dj[$t] = ereg_replace("</SERVERTITLE>.*", "", $pageed);
        $r++;
    }
    //end song info
    fclose($scfp);
}

//display stats
if ($streamstatus == "1") {
    //you may edit the html below, make sure to keep variable intact

    $str = $song[0];
    $artist_name = $song[0];
    $posted = preg_replace('/ -.*/', '', $artist_name);
    $manual_referer = 'http://itunes.com/';
    $itunes_song = str_replace("'", '', $str);
    $args = array(
        'term' => $str,
        'entity' => 'song',
        'limit' => '1',
    );

    $url = "https://itunes.apple.com/search?";
    foreach ($args as $key => $val) {
        $url .= $key . '=' . rawurlencode($val) . '&';
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $manual_referer);
    $body = curl_exec($ch);
    curl_close($ch);

    $json = json_decode($body, true);
    $results = $json['results'];

    foreach ($results as $result) {
        $res = $result['artworkUrl100'];
    }

    $str_fixed = str_replace('-', '</div><div style="width: 1px; height: 16px;"></div><div style="font-size: 16px; font-weight: bold; margin-left: 10px; float: left; color: #FFF;">', $str);

    if ($posted == "sweeper" || $posted == "DRY") {
        echo '
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
    <meta http-equiv="refresh" content="15" />
    </head>
    <span style="float: right; color: #FFF; font-size: 16px; direction: rtl; font-weight: bold; margin-top: 10px;">פרסומות וחוזרים!</span>';
    } else if (strlen($res) == 0) {
        echo '
    <p align="center"><img src="media/images/nopic.jpg" style="max-width: 100px; max-height: 100px;" align="left">
    <b></b> <div style="font-size: 14px; margin-left: 10px; color: #FFF; float: left;">' . $str_fixed . '</div></p><b>
    ';
    } else {
        echo '
    <p align="center"><img src="' . $res . '" style="max-width: 100px; max-height: 100px;" align="left">
    <b></b> <div style="font-size: 14px; margin-left: 10px; color: #FFF; float: left;">' . $str_fixed . '</div></p><b>
    ';
    }
    echo '
    </p>
    </body>

    </html>';
}
if ($streamstatus == "0") {
    echo '
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
    <meta http-equiv="refresh" content="15" />
    </head>
    <span style="float: right; color: #FFF; font-size: 16px; direction: rtl; font-weight: bold; margin-top: 10px;">אין שידורים כעת!</span>';
}
?>

1 个答案:

答案 0 :(得分:0)

您的代码应该没问题,您可以查看str_replace here

的行为

我只是假设你在检查

时引用了错误的变量
$itunes_song = str_replace("'", '', $str);
$args = array(
    'term' => $itunes_song,
    'entity' => 'song',
    'limit' => '1',
);

echo $song_itunes;

我认为$song_itunes应该是$itunes_song

我的另一个问题是,您仍在使用$str,而您需要在此处使用新创建的$itunes_song

$str_fixed = str_replace('-', '</div><div style="width: 1px; height: 16px;"></div><div style="font-size: 16px; font-weight: bold; margin-left: 10px; float: left; color: #FFF;">', $str);

或在这里

$args = array(
    'term' => $str,
    'entity' => 'song',
    'limit' => '1',
);