基于相同分隔符的php字符串解析

基于相同分隔符的php字符串解析,php,string,Php,String,由于可怕的和不一致的格式由某个网站,我们收到的数据,我需要解析出以下字符串 $s = array('James Cussen\'s Destructor Bot', 'Andre Riverra\'s Quick-Runner - San francisco', 'Kim Smith\'s - NightBot'); 进入: 詹姆斯·库森:析构函数机器人 安德烈·里维拉:快跑者 金·史密斯:夜猫子 我的问题是,将带有两个“-”的行解析为相应的Own

由于可怕的和不一致的格式由某个网站,我们收到的数据,我需要解析出以下字符串

$s = array('James Cussen\'s Destructor Bot',
           'Andre Riverra\'s Quick-Runner - San francisco', 
           'Kim Smith\'s - NightBot');
进入:

詹姆斯·库森:析构函数机器人
安德烈·里维拉:快跑者
金·史密斯:夜猫子

我的问题是,将带有两个“-”的行解析为相应的Owner:name格式的最佳方法是什么

$bot ='';
$creator = '';

foreach($s as $parse)
{

   //if string contains '
    if(strpos($parse,'\'') !== false)
            {

              if(substr_count ($parse, '-') > 1)
              {
                  $arr =  explode('\'', $parse);


                  $line =  trim(substr($arr[1], 1));


              }
               if(strpos($parse,'–') !== false)
                 {
                 $temp = explode('–',$parse);
                 }
                 else
                 {
                 $temp =  explode('-', $parse);
                 }

                $arr =  explode('\'', $temp[0]);
                $creator = $arr[0];

                $bot =  trim(substr($arr[1], 1));


            }

    echo $creator.':'.$bot;
    echo '<br>';
}
$bot='';
$creator='';
foreach($s作为$parse)
{
//如果字符串包含'
if(strpos($parse,'\'')!==false)
{
if(substr_count($parse,'-')>1)
{
$arr=explode(“\”$parse);
$line=trim(substr($arr[1],1));
}
if(strpos($parse,'-')!==false)
{
$temp=分解('-',$parse);
}
其他的
{
$temp=分解('-',$parse);
}
$arr=爆炸('\'',$temp[0]);
$creator=$arr[0];
$bot=trim(substr($arr[1],1));
}
echo$creator.':'.$bot;
回声“
”; }
这在将来肯定会失败,因为数据传输的格式不一致,但嘿,至少它现在可以工作了

foreach ($s as $entry):
    list($creator, $bot) = explode('\'s', $entry);
    if (substr($bot, 0, 3) !== ' - '):
        $bot = substr($bot, 0, strpos($bot, ' - '));
    else:
        $bot = substr($bot, 3);
    endif;
    echo $creator . ': ' . $bot . '<br>';
endforeach;
foreach($s作为$entry):
列表($creator,$bot)=分解('\'s',$entry);
如果(substr($bot,0,3)!='-'):
$bot=substr($bot,0,strpos($bot,'-');
其他:
$bot=substr($bot,3);
endif;
echo$creator。“:”$机器人。”
'; endforeach;
詹姆斯·库森(James Cussen)的条目真的缺少一个
-
,还是一个打字错误?如果是这样的话,那就相当困难了。它确实缺少了一个-,但这并不是主要的问题,因为我只能从‘到字符串的末尾,它是一个有两个’-’的,我不知道如何处理,因为我不能再在’-‘上爆炸了。