PHP txt File Manipulations - php

I'm trying to create a simple one page php website that does the following.
Displays data from a txt file and then converts it into links.
(A line from the text file would be so if the line is "TextEdit: 1.9" ApplicationName = TextEdit)
So basically I need to add the url strings together with the application names in the desired locations and then redirect to that page. Hopefully ending up with something like:
Link[urlportion+appname+urlportion]
I am getting my data from a txt file that is formatted in the format of:
TextEdit: 1.9
AppleScript Editor: 2.6
Arduino: 1.0.5
TextWrangler: 4.5.3
etc.
I can display the code in my page using:
<?php
foreach(glob("log.txt") as $filename) {
$file = $filename;
$contents = file($file);
$string = implode("<br>",$contents);
echo $string;
echo "<br></br>";
}
?>
That works beautifully my question is how do I separate the txt file into pieces so I can concatenate it with my url and display it.

If it's simple you can just use a text file and delimit your strings. You're going to need a server-side technology to do this - PHP, ASP.NET, etc. You haven't posted what your server-side technology is, but this can't be done without it.

Related

php code not working from php str_replace function inside

i want run php code inside str_replace();
examples:
data example is here:
"file":"https://example.com/hls2/02/00008/,kioaker9jwxw_n,.urlset/master.m3u8%22,%22id%22:%22kioaker9jwxw"}
My php code example:
$replace1 = str_replace('"file":"', '"file":"https://otherdomain.com/palx/test/?v=\<?php echo base64_encode("', $data);
$replace2 = str_replace('m3u8', 'm3u8")?\>', $replace1);
Edit;::::
i want change "example.com/m3u8" to other.com/ex.php?v=example.com/m3u8 but base64 encoded from data.
data contains one more links.
And contains tokens so changing file to file.
i can't touch /hls2/02/.... because changing file to file, this is auto bot.

PHP Search System that reads .txt and outputs every line that has a specific string in it

So I have this site where I want to create a listing of best torrent Magnet URI alternatives.I'd rather list everything from some .html file:
torrent1:name_of_torrent
torrent2:name_of-torrent
...
and have PHP check for the $_POST['search_torrent'] and search that data inside the .html, if any of the specific data/name was found in any of those lines, then output that entire line as a item using foreach().
I did something like this, but the host I'm trying to accomplish this on doesn't allow exec() nor shell_exec().
<?php
if (isset($_POST['search_torrent'])) {
$search_torrent = $_POST['search_torrent'];
$torrent_file = "read.txt";
echo nl2br(shell_exec("type ".$torrent_file."| find /I \"".$search_torrent."\""));
?>
I know I know... I could use database but I'm just not into that at this moment.
Thanks!
Read the file lines into an array and grep that:
foreach(preg_grep('/'.preg_quote($_POST['search_torrent'], '/').'/i', file('read.txt')) as $line) {
echo "$line<br>";
}

Replace HTML elements content of a PHP/HTML file with PHP

Problem
I'm trying to edit HTML/PHP files server side with PHP. With AJAX Post I send three different values to the server:
the url of the page that needs to be edited
the id of the element that needs to be edited
the new content for the element
The PHP file I have now looks like this:
<?php
$data = json_decode(stripslashes($_POST['data']));
$count = 0;
foreach ($data as $i => $array) {
if (!is_array($array) && $count == 0){
$count = 1;
// $array = file url
}
elseif (is_array($array)) {
foreach($array as $i => $content){
// $array[0] = id's
// $array[1] = contents
}
}
}
?>
As you can see I wrapped the variables in an array so it's possible to edit multiple elements at a time.
I've been looking for a solution for hours but can't make up my mind and tell what's the best/possible solution.
Solution
I tried creating a new DOMElement and load in the html, but when dealing with a PHP file, this solution isn't possible since it can't save php files:
$html = new DOMDocument();
$html->loadHTMLFile('file.php');
$html->getElementById('myId')->nodeValue = 'New value';
$html->saveHTMLFile("foo.html");
(From this answer)
Opening a file, writing in it and saving it comes is another way to do this. But I guess I must be using str_replace or preg_replace this way.
$fname = "demo.txt";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$content = str_replace("oldword", "newword", $content);
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
(From this page)
I read everywhere that str_replace and preg_replace are risky 'caus I'm trying to edit all kinds of DOM elements, and not a specific string/element. I guess the code below comes close to what I'm trying to achieve but I can't really trust it..
$replace_with = 'id="myID">' . $replacement_content . '</';
if ($updated = preg_replace('#id="myID">.*?</#Umsi', $replace_with, $file)) {
// write the contents of $file back to index.php, and then refresh the page.
file_put_contents('file.php', $updated);
}
(From this answer)
Question
In short: what is the best solution, or is it even possible to edit HTML elements content in different file types with only an id provided?
Wished steps:
get file from url
find element with id
replace it's content
First of all, you are right in not wanting to use a regex function for HTML parsing. See the answer here.
I'm going to answer this question under the presumption you are committed to the idea of retrieving PHP files server-side before they are interpreted. There is an issue with your approach right now, since you seem to be under the impression that you can retrieve the source PHP file by the URL parameter - but that's the location of the result (interpreted PHP). So be careful your structure does what you want.
I am under the assumption that the PHP files are structured like this:
<?php include_some_header(); ?>
<tag>...</tag>
<!-- some HTML -->
<?php //some code ?>
<tag>...</tag>
<!-- some more HTML -->
<?php //some code ?>
Your problem now is that you cannot use an HTML reader (and writer), since your file is not HTML. My answer is that you should restructure your code, separating templating language from business logic. Get started with some templating language. Afterwards, you'll be able to open the template, without the code, and write back the template using a DOM parser and writer.
Your only alternative in your current setup is to use the replace function as you have found in this answer. It's ugly. It might break. But it's definitely not impossible. Make backups before writing over your own code with your own code.

pass mysql value from php page to a joomla article and display relevant data inside of a joomla article

we are developing an application.website is being developed by joomla.Admin panel is being developed using a pure php.on index page(joomla), we are displaying some details from the backend.
my question is this, when we click on one of the records on that page can we display the relevant data inside of a article?
Hope i asked the question clearly.
please share your thoughts with us.
thanks in advance
Yes, you can do this, if I understand your question correctly.
Open up Joomla's main index.php. This is the index.php in the html root, not the index.php in one of the template folders.
Near the bottom of the file, or maybe the very last line you will see something like this:
// Return the response.
echo $app
Replace this line with the following:
// Return the response.
// parse $app for server side includes statements and execute them
// note: this will only work for executable code, it will not import text or html files
// we would need to check to see if the file were executable, then read it rather than execute it if it were not
$output = $app;
while(ereg('(<!--#include virtual="([^&]+)" -->)',$output,$groups)){ // extract the ssi command and the command
$i = 0;
while(!$inline){ // sometimes exec() fails for want of memory so we try a few times
exec($groups[2],$array); // get the output from the command
foreach ($array as $element) // concatenate the lines of output into a single string
$inline = $inline . $element . "\n"; // appending a new line makes the html source more readable
$i++;
if($inline | $i > 5)
break;
sleep(1);
}
$output = ereg_replace($groups[1],$inline,$output); // replace the ssi command with the output
}
echo $output;
This will allow you to place a standard server side includes statement in your article. Fore example if you want to execute a php file in the same directory as your index.php and the file is called dynamic_content.php you would type this in your article:
<!--#include virtual="dynamic_content.php"-->
The output of that script will then be included in the text of the article. You can have multiple ssi commands in the same article.

Download a dynamically generated file using PHP

This might sound really "nooby" but I need to find a way for PHP to download an XLS file to a server folder. This file is not stored in another server, it is dynamically generated with another PHP script.
This is what I got from browsing the web but it's not working:
<?php
$url = "http://localhost/ProyectoAdmin/admin/export_to_excel.php?id=1&searchtype_id=2";
$local_file_path = './xls_tmp/Report.xls';
$xlsFile = file_get_contents($url);
file_put_contents($file_path,$xlsFile);
?>
I'd really appreciate any hint.
You're missing an end quote on your second line.
It should be: $local_file_path = './xls_tmp/Report.xls';

Categories