Arrays 在php中处理文本并转换为字符串而不删除文本格式

Arrays 在php中处理文本并转换为字符串而不删除文本格式,arrays,text,Arrays,Text,我将以下文本作为输入文本: HEADER:This is title 1 HEADER:This is title 2 HEADER: This is title 3 我想删除“HEADER:”并将文本转换为长字符串,并保留文本格式(不删除空格),如下所示: This is title 1 This is title 2 This is title 3 假设每行的长度固定为30个字符(包括空格)。 下面是我的代码 $string

我将以下文本作为输入文本:

HEADER:This is title 1
HEADER:This is         title 2
HEADER:        This is title 3
我想删除“HEADER:”并将文本转换为长字符串,并保留文本格式(不删除空格),如下所示:

This is title 1      This is       title 2      This is title 3
假设每行的长度固定为30个字符(包括空格)。 下面是我的代码

$string = $this->input->post(inputText);

//move the string to array 
$Text = array($string);
$lines = count($Text);

$x=0;
while($x<=$lines)
{
   //Remove "HEADER:" Length is = 7
   $newText[$x] = array(substr($Text[$x],7));
   $x++;
}

$stringText = implode("\n",$newText);

echo $stringText;
$string=$this->input->post(inputText);
//将字符串移动到数组中
$Text=数组($string);
$lines=计数($Text);
$x=0;

当($xHey Julie)出现问题时,explode函数正在删除字符串中的空白,因此

$string = str_replace(" ", "-", $string);
现在,您将获得包含所有空格的子字符串,这些子字符串将替换为-

$Texts = explode('HEADER:',$string);
foreach($Texts as $key=>$data)
{
  $Texts[$key] = str_replace('-',"&nbsp",$data);
}
这样,您将拥有一个数组,其中包含数组中所需的子字符串

更新

$stringPhp = str_replace(" ", "-", $string);
$Texts = explode('HEADER:',$string);
$TextPHP = explode('HEADER:',$string);
foreach($TextPHP as $key=>$data)
    {
      $TextsPHP[$key] = str_replace('-',"&nbsp",$data);
    }
    //U can use $TextsPHP for you php I am sure it will not give you any troubles.

for html suppose you want to print $Texts[1] element.

<input type="text" value="<?php echo $Texts[1];" />
$stringPhp=str_replace(“,”-“,$string);
$text=explode('HEADER:',$string);
$TextPHP=explode('HEADER:',$string);
foreach($TextPHP作为$key=>$data)
{
$TextsPHP[$key]=str_替换(“-”、“ ”、$data);
}
//你可以使用$TextsPHP为你php我相信它不会给你任何麻烦。
对于html,假设您要打印$text[1]元素。

您是否检查了$Text返回的内容。接下来的计数是多少$Text值是正确的,但似乎计数是错误的。它返回值1您的$Text将在一个索引中返回整个字符串。实际上,您无法正确分解字符串。那么,我应该先分解,然后删除标头,然后再重新分解?我成功地将分解$Text后t 3行,但没有得到想要的输出..空格都没有..我又卡住了..在我执行str_替换('-',“ ',$data)后;我的数据包含以下内容:这是标题1这是nbsp标题2这是标题3@Julie现在哪里出了问题?我不明白你想说什么。请解释一下这个 is title 1 This&bspis&title&2&nsp&This is title 3i在我的浏览器中显示了空格..但是我得到了这个输出n我的输出文件..它包含