我有一个字符串如下:
$value[0]='#EXTINF:-1 tvg-name="title 1" tvg-logo="./Logo1.png" group-title="groupTitle1",title 1';
我想获取tvg-name =,tvg-logo =,group-title =的数据以及最后一个“,”之后的另一个值,在这种情况下为title 1
。我想将这些数据作为变量放置以供以后使用。谁能告诉我最好的办法呢?
答案 0 :(得分:1)
这是一个不使用preg_match
的示例,但应该可以获得预期的结果
<?php
function scrape_value($data, $key){
$start = $key.'="';
$end = '"';
$data = stristr($data, $start); // Stripping all data from before $start
$data = substr($data, strlen($start)); // Stripping $start
$stop = stripos($data, $end); // Getting the position of the $end of the data to scrape
$data = substr($data, 0, $stop); // Stripping all data from after and including the $end of the data to scrape
return $data; // Returning the scraped data from the function
}
$value[0] = '#EXTINF:-1 tvg-name="title 1" tvg-logo="./Logo1.png" group-title="groupTitle1",title 1';
echo scrape_value($value[0], 'tvg-name'); echo '<br />';
echo scrape_value($value[0], 'tvg-logo'); echo '<br />';
echo scrape_value($value[0], 'group-title'); echo '<br />';
echo substr($value[0],strrpos($value[0],',')+1);
?>
tvg-name = title 1
tvg-logo = ./Logo1.png
group-title = groupTitle1
上一个值的结果= title 1