Domanda

So I want to be able to paste this into the text area and I want it to write each particular line/word into the text file.

First Name Last Name
Home (123) 345-6789 
51 Street Name 
City, Pr ABC DEF

The text file should write in this format. First Name Last Name!Home (123) 345-6789!51 Street Name!City! Pr !ABC DEF! (note the exclamation marks, spaces and coma). The program should be able to read all the words from line 1,2,3 regardless of how many words there are. Line 4 will be exactly in that format with 4 separate words. I need it to output in that particular format I listed above. This is what I'm confused about. If anyone has a solution for this I would greatly appreciate it. I've tried many things and I just can't seem to get my code to work. I'm new to this so if you could include as much as possible that would be nice. Thank You.

È stato utile?

Soluzione

By simply making use of PHP's string functions.

<?php
$str='First Name Last Name
Home (123) 345-6789
51 Street Name
City, Pr ABC DEF';
$new_str="";
foreach(explode(PHP_EOL,$str) as $val)
{
    if(strpos($val,',')!==false)
    {
        $val=trim(str_replace(',','',$val)); //<--- For the space thing
        $val=explode(' ',$val);
        $new_str.=implode('!',$val)."!";
    }
    else
    {
        $new_str.=trim($val)."!"; //<--- Added here
    }
}
echo $new_str;

OUTPUT :

First Name Last Name!Home (123) 345-6789!51 Street Name!City!Pr!ABC!DEF!
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top