Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add new line before string match? 2

Status
Not open for further replies.

silkk

Programmer
Jul 14, 2008
16
CZ
Hi guys,
I have a text file which after rtrim has only one empty space between the string.
My goal is to reorder the text by using some matching function.
Example:
"Head sad asdg fasfa hgsdfsdf asdasd fasdf asdasd asdasdga asdas sadasd asdasd Head dasdgsdf seaseda sadas ....."
I want to divide the text on sections and to replace the empty spaces with commas for further SQL injection.

Help will be apreciated.
 
You can use str_replace to change all the spaces into commas.

$mystring=str_replace(" ",",",$original_string);

But I'm a little confused, first you say there's a single space, and then you want to change multiple spaces into commas? which is it?

Ad then where does the new line in your title come into play?




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I see use of divide & section. Then is see the example where there are 2 distinct "Head" words in all the gibberish. This leads me to think that the OP might want to break the line before the Head word THEN replace all the spaces with commas. We might try this:

Code:
$new_string = null;
$exploded_string = explode(' ', $original_string);
foreach($exploded_string as $value) {
  if($value == "Head") {
    if(!empty($new_string)) {
      $new_string .= "\n";
    }
    
    $new_string .= $value;
  } else {
    $new_string .= ",".$value;
  }
}

--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
My task is to convert text file (something like bank transaction document with lots of spaces between the strings) into a file similar to a CSV one.
I have repeating strings like HEAD and FOOT and I want to divide all the info to be in sections,starting with HEAD and ending with FOOT.
After this is done I will create separate DB tables based on the sections created.

 
Thanks for the posts,guys :)
Zeland,this was helpful :)
 
Hi

Here on Tek-Tips we used to thank for the received help by giving stars. Please click the

* [navy]Thank zeland
for this valuable post![/navy]


at the bottom of zeland's post. That way you both show your gratitude and indicate this thread as helpful.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top