如何重新格式化字符串

时间:2016-01-09 04:53:55

标签: php html

我有以下字符串:

  

1)什么是印度的首都?a)bangloreb)delhic)Hyderabad2)什么是telangana的资本?a)hydb)khammamc)warangal

如何将其更改为:

  1)什么是印度的资本?   A)班加罗尔   B)德里   C)海得拉巴

     2)telangana的资本是什么?   A)路政署   B)坎曼   C)waranagal

2 个答案:

答案 0 :(得分:0)

<?php
$data ="1)what is capital of india?a)bangloreb)delhic)Hyderabad2)what is capital of telangana?a)hydb)khammamc)warangal";
$data= str_replace("a)"," A)","$data");
$data= str_replace("b)"," B)","$data");
$data= str_replace("c)"," C)","$data");
$data= str_replace("d)"," D)","$data");

echo str_replace("2)"," <br/>2)","$data");

&GT;

答案 1 :(得分:0)

尝试以下代码:

$data = "1)what is capital of india?a)bangloreb)delhic)Hyderabad2)what is capital of telangana?a)hydb)khammamc)warangal";
$data = preg_replace('/([0-9]+)\)/', "<br/>", $data);
$data = array_filter(explode("<br/>", $data));
foreach ($data as $key => $val) {
    $val = str_replace("a)", " A)", $val);
    $val = str_replace("b)", " B)", $val);
    $val = str_replace("c)", " C)", $val);
    $val = str_replace("d)", " D)", $val);
    echo $key . ") " . $val . "<br />";
}