Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Example file:

Aiken, L. S., & West, S. G. (1991). Multiple Regression: Testing and Interpreting
        interactions. Sage Publications.
  Bai, X., Yao, W., & Boyer, J. E. (2012). Robust fitting of mixture regression
        models. Computational Statistics & Data Analysis, 56 (7), 2347-2359.
  Bartolucci, F., & Scaccia, L. (2005). The use of mixtures for dealing with
        non-normal regression errors. Computational Statistics & Data Analysis,
        48 (4), 821-834.


If I do
file("example")
,Its considering like below:

1st line- Aiken, L. S., & West, S. G. (1991). Multiple Regression: Testing and Interpreting
              2nd line- interactions. Sage Publications

but it should be in a single line. So, I need to make that two lines as single line based on the space which second line have.As such I need all the reference list to be structured.

If I do like this,

$data = str_replace("\r\n", "", $data); 

then all the lines will become as single line. I want each reference to be single line.

I tried,
$f=file("example")
for($i=0;$i<count($f);$i++){
$f[$i] = str_replace("\r\n", "", $f[$i]);
????
//should replace the new line strings to single line til it finds no space in the
 //new line string so that i can structure second reference in a second line and so
 //on.
}
Posted

1 solution

If your PHP supports the regex functions, then it's pretty easy:
PHP
$data = preg_replace('/\r\n\s+/m', ' ', $data);
$lines = explode("\r\n", $data);


The regex '/\r\n\s+/m' matches the "\r\n" line ending followed by at least one character of whitespace on the next line.
 
Share this answer
 
v2
Comments
Vidhya Raju 20-Dec-14 5:41am    
How to take two lines as single data to parse into str_replace("\r\n ", " ", $data).For example,In first reference two lines is a data to replace a new line charac.So, If I use file() it will take first line as single data second line as second data therefore how to replace new line charac between these two lines
Graham Breach 20-Dec-14 9:09am    
You could use file_get_contents() to read in the whole file as a string, then split the string into lines using preg_split() or explode().
Vidhya Raju 22-Dec-14 0:18am    
If I use file_get_contents to read whole file into a string then how can I split the lines using explode()?By which delimiter I can explode whole string into back to form
Graham Breach 22-Dec-14 4:06am    
Using "\r\n" as a delimiter you can split it into lines - I've updated my answer with the extra line of code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900