現在の曲の結果を含む $ NowPlaying に対して呼び出す変数を分割したいと考えています。以下を共有したいと思います - $ artist
$ title を含む 2 つの新しい変数を取得します。検索して解決策を見つけようとしましたが、少しの支援と助けに感謝して失速しました
5 に答える
5
<?php
// Assuming $NowPlaying is something like "J. Cole - Chaining Day"
// $array = explode("-", $NowPlaying); //enter a delimiter here, - is the example
$array = explode(" - ", $NowPlaying); //DJHell pointed out this is better
$artist = $array[0]; // J. Cole
$song = $array[1]; // Chaining Day
// Problems will arise if the delimiter is simply (-), if it is used in either
// the song or artist name.. ie ("Jay-Z - 99 Problems") so I advise against
// using - as the delimiter. You may be better off with :.: or some other string
?>
于 2013-08-01T21:00:59.447 に答える
1
php
爆発()関数を使用する
$str_array = explode(' - ', $you_song);
// then you can get the variables you want from the array
$artist = $str_array[index_of_artist_in_array];
$title = $str_array[index_of_title_in_array];
于 2013-08-01T21:04:34.153 に答える
0
私は通常、次のようなことをします:
<?php
$input = 'Your - String';
$separator = ' - ';
$first_part = substr($input, 0, strpos($input, $separator));
$second_part = substr($input, (strpos($input, $separator) + strlen($separator)), strlen($input));
?>
私はいくつかの分割文字列の質問を見てきましたが、PHP文字列関数の使用を提案する人は誰もいません。これには理由がありますか?
于 2013-08-31T17:27:47.183 に答える