Read form text file and write in html [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a text file on my local server. I want to read content data of this text file and write it into textbox in a html .
Could you please help me?

$data = file_get_contents('yourfile.txt');
echo '<textarea>', htmlspecialchars($data), '</textarea>';
The file_get_contents() loads the data, and then you just need to echo it. Using htmlspecialchars() takes care of escaping any of the data, as needed in the context of HTML.

$textAreaContent = "";
$fp = fopen("read_file.txt", "r");
// If file exist and opened
if($fp)
{
while (($buffer = fgets($fp, 1024)) !== false)
$testAreaContent .= $buffer;
}

Related

How to print a String from a json file in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've a problem.
I want to print out a string the string is in this JSON file with a php scipt:
http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy
it's: data / description
i hope you understand what i mean.
Use jsondecode php function.
$url = 'http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy';
$jsondata = file_get_contents($url); //json data from url
$data = json_decode($jsondata,1); // convert json data to array
echo $data['data']['description']; // access data using keys of array, mapped to properties in json
using json_decode
$json_uri = 'http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy';
$json = file_get_contents($json_uri);
$data = json_decode($json,1); // convert json data to array
$count = 1;
foreach ($data['data'] as $key => $info) {
echo $count++.' - '.$info['description'].'<br>';
}

php reading textfile word by word [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a textfile which contain words like that:
Yigit Ozkavci 19
Efe Ozkavci 18
Yeni Bar 35
.
.
.
They are exist in database and also in textfile, what I want to do is to read those datas and get them in seperate variables, I know how to read a file line by line, but with a foreach loop can I echo them seperately and send data to another php file ?
Why not just make use of file() ? It reads entire content inside an array.
<?php
$arr = file('yourfile.txt');
You can access Yigit Ozkavci 19 by echo $arr[0]; and others so on...
If you want to process it... you can just run it on foreach like this
foreach($arr as $val)
{
echo strtoupper($val); // Just a demonstration ..
}
<?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . $line . "<br />\n";
}
?>
Hope This Helps :)

convert short youtube url to full url [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi all i am looking for a simple way to check if a string equals an url like this:
http://youtu.be/WWQZ046NeUA
To convert it to a proper youtube url like this:
http://www.youtube.com/watch?v=WWQZ046NeUA
If not to leave it alone, what's the simplest way to do it in php?
You can use this preg_replace call:
$u = 'http://youtu.be/WWQZ046NeUA';
$r = preg_replace('~^https?://youtu\.be/([a-z\d]+)$~i', 'http://www.youtube.com/watch?v=$1', $u);
str_replace should work wonders.
$url = ''; //url you're checking
$ytshorturl = 'youtu.be/';
$ytlongurl = 'www.youtube.com/watch?v=';
if (strpos($url,$yturl) !== false) {
$url = str_replace($ytshorturl, $ytlongurl, $url);
}

Naming files without extensions in PHP, how to add an extension to a file? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am working in PHP, and I have some files without extensions.
Example:
MYfolder
firstfile
secondfile
otherfile
And so on.
I want to list the files, and give it all ".xml" extension. How do I do it in PHP?
You could try something like this:
<?php
if ($handle = opendir('/MYfolder')) {
while (false !== ($fileName = readdir($handle))) {
rename($fileName, $filename.'xml');
}
closedir($handle);
}
?>
On Linux/Unix box:
exec('for F in your/directory/*; do mv $F{,.xml}; done');

PHP: Add line to text file from URL variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have never done any php, but I am trying to do something very simple.
So basically what I am trying to achieve is that add a line to already existing text file from url. To make it easier to understand, here's an example:
I have text file, what contains the following:
127.0.0.1-USA-Admin
127.0.0.1-SWE-Admin
127.0.0.1-CA-Admin
so after I go to link:
example.com/index.php?ip=127.0.0.1&Co=USA&Usr=Admin
The text file would be updated, and it would look like this:
127.0.0.1-USA-Admin
127.0.0.1-SWE-Admin
127.0.0.1-CA-Admin
127.0.0.1-USA-Admin
How to achieve this?
I am sorry if it's a beginner question, but I've never done anything in PHP.
I assume it has something to do with $_GET -thing.
Please help :)
Here it is,
$ip = $_GET["ip"];
$co = $_GET["co"];
$usr = $_GET["usr"];
$file = "yourfile.txt";
file_put_contents($file, "$ip-$co-$usr", FILE_APPEND | LOCK_EX);
For more information, please check
http://php.net/manual/en/function.file-put-contents.php
Lets say that the text file name is textfile.txt. Bellow is the php code:
<?php
$fh = fopen('textfile.txt', 'a');
fwrite($fh, $_GET['ip'].'-'.$_GET['Co'].'-'.$_GET['Usr']."\n\r");
fclose($fh);
?>
You've not given any code to correct/fix. And I'm not going to write it for you.
Here are the functions you'll most likely need:
You'll need a couple of functions.
file_put_contents http://php.net/manual/en/function.file-put-contents.php
To append to the file.
parse_url http://php.net/manual/en/function.parse-url.php
If you get stuck, paste your code into your question.
You can use file_put_contents:
file_put_contents(
'your-file.ext',
sprintf( "%s-%s-%s", $_GET['ip'], $_GET['Co'], $_GET['Usr'] ),
FILE_APPEND
)
The code should be
$contents = file("myfile.txt");
$contents[] = $_GET["ip"]."-".$_GET["Co"]."-".$_GET["Usr"];
file_put_contents('myfile.txt', $contents);

Categories