2017-08-15 37 views
-1

我将如何去除和<b>标签,从curl输出下面?我试图strip_tags但我不知道如何重新排列顺序字符串标签和从输出重新排列的顺序

<?php 
$ch = curl_init("http://beta.test123.com/archive.csv?s=BLOGS&f=lc1"); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
curl_exec($ch); 
curl_close($ch); 
?> 

卷曲输出

"4:01pm - <b>n1234</b>",+0.50 

努力实现以下格式,输出到blog.inc文件

n1234 
+0.50 
4:01 PM 

回答

1
$result = strip_tags($out); 
$result = str_replace(["- ", '"'], '', $result); // remove unnecessary chars 
$result = str_replace(',',' ', $result); // change comma to space for explode 
$array = explode(' ', $result); // explode by space 

//here result - array with needed values 
$array[1] // should be n1234 
$array[0] // should be 4:01pm 
$array[2] // should be +0.50 
1

也许这样?
我使用regex来获取字符串的各个部分,并使用strip_tags()删除粗体标记。

要重新安排项目到您的订单我使用array_shift()

$str = '"4:01pm - <b>n1234</b>",+0.50'; 

preg_match('/\"(.*?)\s-\s(.*?)\",(.*)/', strip_tags($str), $matches); 
unset($matches[0]); // unset [0] because that is the full match. 
$matches[] = array_shift($matches); // takes first item and makes it last. 

echo implode("<br>\n", $matches); 
//var_dump($matches); 

输出:

n1234 
+0.50 
4:01pm 

https://3v4l.org/smQIV

EDIT;我现在看到你想要“4:01 PM”并有空格。不知道它是否是一个错字,但下面的代码应该做到这一点,只需在爆发之前添加它。

$matches[2] = date("g:i A", strtotime($matches[2])); 
+0

我试着将你的代码附加到我的存在$ str = $ ch;但没有运气,我错过了什么? – acctman

+0

我不得不添加CURLOPT_RETURNTRANSFER并且还使用'curl_exec($ ch)'......除了行分隔以外,一切似乎都起作用了。它仍然在一条线上 – acctman

+0

对不起,我忘了。使用'
\ n'作为内爆字符串。 – Andreas