Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing text and convert to string without removing the text format in php

Tags:

arrays

text

I have the following text as inputText:

HEADER:This is title 1
HEADER:This is         title 2
HEADER:        This is title 3

I want to remove "HEADER:" and convert the text into long string and keep the text format(without remove the spaces) as sample below:

This is title 1      This is       title 2      This is title 3

Assuming the length for each line is fix to 30 character (include spaces). The following is my code.

$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;

But it seems this code is not working..

like image 386
Julie Avatar asked Apr 15 '26 11:04

Julie


1 Answers

Hey Julie the problem was coming that the explode function was removing the white spaces in the string so

$string = str_replace(" ", "-", $string);

Now you will get your sub string with all white spaces would be replaced by -

$Texts = explode('HEADER:',$string);
foreach($Texts as $key=>$data)
{
  $Texts[$key] = str_replace('-',"&nbsp",$data);
}

By this you will have a array with sub strings you required in a array

UPDATE

$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];" />

Now you will see that in html output, those blank spaces would be present

like image 187
Sushant Yadav Avatar answered Apr 18 '26 06:04

Sushant Yadav