Jump to content

Include plain text file


eiranix
 Share

Recommended Posts

I know little about php so bear with me...

 

I have put together this code from a few examples online that almost does what I want.

<?php$text = file_get_contents('list.txt'); // get contents of file$html = preg_replace('/s(w+://)(S+)/', '<a href="12">12</a>', $text);// replace URLs with <a href...> elements$html = preg_replace("/[rn]+/", "n", $html); // remove blank lines$html = preg_replace("/^/m", "<li>", $html); // add <li> to start of each lineecho str_replace("n", "</li>n", $html); // add </li> to end of each line and output?> 

It takes a plain text file and turns each line into an html list item as well as converting url's to html links.

 

Can anyone tell me -

 

1) Is this a valid way to do it (any mistakes)?

 

2) If there is a url in the txt file and its on its own line, it'll end up being it's own list item. How can i join it to the list item above it?

 

for example, my text file is this:

list item 1list item 2http://www.google.comlist item 3list item 4list item 5

and I want the url to be a part of 'list item 2' but with a <br/> so it looks like this:

 

Edited by eiranix
Link to comment
Share on other sites

This should work out :)

<?php$text = $text = file_get_contents('list.txt'); // get contents of file//Explode new lines to array$text_contents = explode("n", $text);//Run a foreach to manipulate an array valueforeach($text_contents as $content) {	$check_url = parse_url($content);	//if its a URL add the <a ...	if(isset($check_url['host'])) {		$content = '<a href="'.$content.'">'.$content.'</a>';	}	//Add <li> to all content and pack them back to array	$output[] = '<li>'.$content.'</li>';}//Expload array back to stringsecho implode('', $output);?>
Link to comment
Share on other sites

Thanks for the reply. :)

 

I combined some elements from my first attempt with yours and it seems to be working great now.

 

Are there any problems with this?:

<?php$text = $text = file_get_contents('list.txt'); // get contents of file$blankless = preg_replace("/[rn]+/", "n", $text); // remove blank lines$text_contents = explode("n", $blankless); //Explode new lines to array//Run a foreach to manipulate an array valueforeach($text_contents as $content) {   $check_url = parse_url($content);	//if its a URL add the <a ...	if(isset($check_url['host'])) {		$content = '<br/><a href="'.$content.'">'.$content.'</a>';	} 	//Add <li> to all content and pack them back to array	$output[] = '<li>'.$content.'</li>';}$string =  implode("n", $output); //Implode array back to stringsecho str_replace("</li>n<li><br/>", "<br/>n", $string);//combines links with <li> above?>
Link to comment
Share on other sites

Quite well :) seems you don't need the <li> elements because you are replacing it in the output. And for the blank lines, for effective results i suggest you also add it to the foreach loop. Like:

foreach($text_contents as $content) { if($content !== null) {  $check_url = parse_url($content);	//if its a URL add the <a ...	if(isset($check_url['host'])) {		$content = '<br/><a href="'.$content.'">'.$content.'</a>';	} 	//Add <li> to all content and pack them back to array	$output[] = '<li>'.$content.'</li>';}}
Link to comment
Share on other sites

Another thing - Is it possible to run this same script several times on one page with different text files?

I could repeat the whole script each time of course, but I thought there might be a way to have the full script once at the top then have a short line that can call it with a file name variable...

(like what you'd do with a javascript function)

Link to comment
Share on other sites

No, the ($content !== null) still includes the blank lines?

Ye, during the explode it also explode empty lines to array thereby making $content to be null. Now am making it show all lines which the content is not null :)

Link to comment
Share on other sites

Another thing - Is it possible to run this same script several times on one page with different text files?

I could repeat the whole script each time of course, but I thought there might be a way to have the full script once at the top then have a short line that can call it with a file name variable...

(like what you'd do with a javascript function)

The above code can work quite well since the content is of each line

Link to comment
Share on other sites

I made some adjustments, it now removes the blank lines with trim.

 

Is this the correct way to set up a function?:

<?phpfunction list_maker($file){$text = file_get_contents($file); // get contents of file$text_contents = explode("n", $text); //Explode new lines to arrayforeach($text_contents as $content) {   if(trim($content) !== '') { // removes blank lines    $check_url = parse_url($content);    //if its a URL add the <a ...    if(isset($check_url['host'])) {      $content = '<br/><a href="'.$content.'">'.$content.'</a>';    }     //Add <li> to all content and pack them back to array    $output[] = '<li>'.$content.'</li>';  };};$string =  implode("n", $output); //Implode array back to stringecho str_replace("</li>n<li><br/>", "<br/>n", $string);//combines links with <li> above}?><p>This is list 1:</p><ul><?php  echo list_maker('list1.txt');?></ul><p>This is list 2:</p><ul><?php  echo list_maker('list2.txt');?></ul>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...