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..
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('-'," ",$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('-'," ",$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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With