我想拆分一个我调用$ NowPlaying的变量,它包含当前歌曲的结果。我现在想分享以下内容 - 所以我得到两个包含$ artist
$ title的新变量。经过搜索并试图找到解决方案,但已经停下来感激了一点帮助,并提供帮助
答案 0 :(得分: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
?>
答案 1 :(得分: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];
答案 2 :(得分:1)
听起来你想要使用explode()
答案 3 :(得分: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字符串函数。有这个原因吗?
答案 4 :(得分:-1)
list()就是为了这个目的。
<?php
list($artist, $title) = explode(' - ', $NowPlaying);
?>