1

我想拆分一个我调用的变量 $ NowPlaying ,其中包含当前歌曲的结果。我现在想分享以下内容 - 所以我得到了两个包含 $artist
$title 的新变量。已经搜索并试图找到解决方案,但已经停滞不前感谢一点帮助,并帮助

4

5 回答 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 explode()函数

$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 回答
1

听起来你想使用explode()

http://php.net/manual/en/function.explode.php

于 2013-08-01T21:05:22.133 回答
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 回答
-1

list() 正是为此目的而制作的。

<?php
  list($artist, $title) = explode(' - ', $NowPlaying);
?>

http://php.net/manual/en/function.list.php

于 2013-08-01T21:06:00.013 回答