如何以指定的格式显示php字符串

时间:2018-04-28 07:52:48

标签: php string

考虑以下字符串:

  $raw = "'Joy is the serious business of heaven.'.LEWIS,CLIVE STAPLES.1964-01-01.
|'We were not meant to be somebody--we were meant to know Somebody'.PIPER,JOHN STEPHEN.2011-07-17.|'He who sings prays twice.'.Hipponensis,Aurelius Augustinus.430-02-30.
|'The task of the modern educator is not to cut down jungles but to irrigate deserts.'.LEWIS,CLIVE STAPLES.1943-09-23.
|'There is not one blade of grass, there is no color in this world that is not intended to make us rejoice.'.Calvin,John C.1530-10-09.|
'The worth and excellency of a soul is to be measured by the object of its love.'.SCOUGAL,HENRY P.1678-08-23.
|'It is not the strength of your faith but the object of your faith that actually saves you.'.KELLER,TIMOTHY J.2013-01-14.
|'Truth is the agreement of our ideas with the ideas of God.'.Edwards,Jonathan Prtn.1703-09-23.
|'Each day we are becoming a creature of splendid glory or one of unthinkable horror.'.LEWIS,CLIVE STAPLES.1952-02-01.|'At your right hand are pleasures evermore..'.David,Jesse soun.1200-09-29.|'Tolerance is not about not having beliefs. It is about how your beliefs lead you to treat people who disagree with you.'.KELLER,TIMOTHY J.2015-10-23.
|'It is better to lose your life than to waste it.'.PIPER,JOHN STEPHEN.2000-05-33.|
'It is not opinions that man needs: it is TRUTH...'.Bonar Horatius B.1885-02-12.https://www.goodreads.com/author/quotes/133605.Horatius_Bonar|
'Nothing could be more irrational than the idea that something comes from nothing.'SPROUL,CHARLES ROBERT.2006-03-23.
|'He is no fool who gives what he cannot keep to gain that which he cannot lose.'.Elliot,James Phillip.1944-07-26.
";

它包含引号,作者(姓氏,first_name second_name),日期和url_reference  您需要按如下方式显示以下数据(注意:可以单击作者的first_name以在新选项卡上打开url_reference)

  (a). "Quote" - first_name second_name,surname (YEAR).
  (b). .......
  (c). .......
  (For example                                                                                                  *
    a) "The worth and excellency of a soul is to be measured by the object of its love." -  HENRY P,SCOUGAL (1678).                                      *
  )
Summary
 -- Total quotes : total.
 -- Total unique authors : total(list of author surnames - comma separated)

1 个答案:

答案 0 :(得分:-2)

对于格式化输出,请使用集成函数printf

Hereprintf文档。

  

编辑:假设您拥有给定表单中的数据

$data = array("quote" => "It is better to lose your life than to waste it.", 
              "name" => "JOHN STEPHEN", "lastname" => "PIPER", 
              "date" => "2000-05-33", "url" => "URL link");

现在让我们首先实现一个简单的print line函数:

function print_formatted_line($data) {
  printf("'%s' - <a href='%s' target='_blank'>%s</a>, %s (%s)<br>", 
    $data["quote"], $data["url"], $data["name"], $data["lastname"], date('Y', strtotime($$data["date"])));
}

现在您可以按照以下方式打印

print_formatted_line($data); // passing an array to function

由于您的数据不可读,因此我们无法执行和实现任何逻辑,因此我们需要将其转换为更具可读性的内容。为此,我们解析您的$raw字符串。 我为特定示例编写了一个简单的代码,用于从给定的字符串中提取数据。

 $raw = "'Joy is the serious business of heaven.'.LEWIS,CLIVE STAPLES.1964-01-01.
|'We were not meant to be somebody--we were meant to know Somebody'.PIPER,JOHN STEPHEN.2011-07-17.|'He who sings prays twice.'.Hipponensis,Aurelius Augustinus.430-02-30.
|'The task of the modern educator is not to cut down jungles but to irrigate deserts.'.LEWIS,CLIVE STAPLES.1943-09-23.
|'There is not one blade of grass, there is no color in this world that is not intended to make us rejoice.'.Calvin,John C.1530-10-09.|
'The worth and excellency of a soul is to be measured by the object of its love.'.SCOUGAL,HENRY P.1678-08-23.
|'It is not the strength of your faith but the object of your faith that actually saves you.'.KELLER,TIMOTHY J.2013-01-14.
|'Truth is the agreement of our ideas with the ideas of God.'.Edwards,Jonathan Prtn.1703-09-23.
|'Each day we are becoming a creature of splendid glory or one of unthinkable horror.'.LEWIS,CLIVE STAPLES.1952-02-01.|'At your right hand are pleasures evermore..'.David,Jesse soun.1200-09-29.|'Tolerance is not about not having beliefs. It is about how your beliefs lead you to treat people who disagree with you.'.KELLER,TIMOTHY J.2015-10-23.
|'It is better to lose your life than to waste it.'.PIPER,JOHN STEPHEN.2000-05-33.|
'It is not opinions that man needs: it is TRUTH...'.Bonar,Horatius B.1885-02-12.https://www.goodreads.com/author/quotes/133605.Horatius_Bonar|
'Nothing could be more irrational than the idea that something comes from nothing.'.SPROUL,CHARLES ROBERT.2006-03-23.
|'He is no fool who gives what he cannot keep to gain that which he cannot lose.'.Elliot,James Phillip.1944-07-26.
";

$iterData = explode('|', $raw, -1); // divide data by delim parametar "|"
$output = array(); // array to save the formatted data in

foreach($iterData as $key) {
  $quote_rest = explode("'.", $key, 2);  // divide one string into strings [0] => quote, [1] => other data
  $lastname_rest = explode(",",  $quote_rest[1], 2); // divide "[1] => other data" to array "[0] => lastname, [1] => name.date.url"
  $name_date_url = explode(".",  $lastname_rest[1], 3); // finally, extract data from [1] => name.date.url to 

  // create array of extracted data
  $data = array("quote" => substr($quote_rest[0], 1), 
                "name" => $name_date_url[0], 
                "lastname" => $lastname_rest[0], 
                "date" => $name_date_url[1], 
                "url" => $name_date_url[2]);
  // insert data to output array
  array_push($output, $data);

  // print data
  print_formatted_line($data); // pass data to output function
} 

/// implementation...

请注意,现在您已将数据保存在output数组中,您可以在其中实现其余的程序逻辑。