Some PHP code is not working, but some can work - php

I have a question about PHP.
Some of function can run, some can not.
For example
<?php
include("LIB_parse.php");
include("LIB_http.php");
# download the page
$web_page = http_get ($target = "http://www.nostarch.com", $referer = "");
$title_incl = return_between($web_page['FILE'], "<title>", "</title>", INCL);
$title_excl = return_between($web_page['FILE'], "<title>", "</title>", EXCL);
echo "title_incl =" .$title_incl;
echo "\n";
echo "title_excl =" .$title_excl;
?>
The result is
title_incl = title_excl =No Starch Press
My espect is
title_incl = No Starch Press
title_excl =No Starch Press
There are two problems in this code.
Firstly, echo "\n"; is not working.
Secondly, the included title's spring was not showed.
Another example, parse_array the meta also got problem.
<?php
include (\"LIB_parse.php\");
include (\"LIB_http.php\");
#$target = \"https://news.google.com.tw/nwshp?hl=zh-TW&tab=wn\";
$web_page = http_get( $target= \"www.twse.com.tw/ch/index.php\", $regerer = \"\");
$meta_tag_array = parse_array($web_page['FILE'], \"<meta\", \">\");
for($xx = 0; $xx<count($meta_tag_array); $xx++)
echo $meta_tag_array[$xx].\"\n\";
?>
The result is nothing, I expect it should show all meta contents in the console.
I am using Eclipse Luna with PHP plug-in.
Also installed appserv2.5.10
Apache 2.2.8
PHP 5.2.6
MySQL 5.0.51b
phpMyAdmin-2.10.3
Please help. Really thanks.

Related

(PHP) How do I properly concatenate a list of arrays into a string for file_put_contents?

I've been playing around with a php input function that will build a html page for a game, something like a wiki. All is going fine and dandy but when trying to build an array into a string it's passing back some funny errors.
It's happening to every one that involves an array and I've snipped some code out that works independently to save me time, but just can't figure it out. I'm using the Linux Terminal to run the script and when I set it to echo inside the foreach loop it does it just fine, it just won't when I try building it into a HTML file.
Here is the input script:
echo "\nHow many ranks were there (number)?:\n";
$facInputRankLimit = readline();
echo "Please read carefully and supply the ranks in descending order (HIGHEST > LOWEST):\n";
$facInputRankCount = 0;
$facInputRankString = "";
while ($facInputRankCount < $facInputRankLimit) {
echo "Enter a rank:\n";
$facInputRanks[$facInputRankCount] = readline();
$facInputRankCount++;
}
foreach ($facInputRanks as $facInputRankList) {
$facInputRankString .= $facInputRankList.PHP_EOL;
}
Then I'll build it into a multi-line echo (rather than appending every single block of code):
$facBuildPage = <<<EOT
<?php
\$facRankLimit = '$facInputRankLimit';
\$facRanks = '$facInputRankString';
include('faction2.html');
?>
EOT;
The variables with the backslashes will then be built into "$facFileName.php" and will be a set of variables inputted through this script (input2.php), which will each also include the same html page.
To top it off I'm getting really weird results... if I create 5 ranks, each as "1 2 3 4 5", I actually get "1 2 3" with two vertical linebreaks in between.
Edit: Snipped some of it, I didn't want to risk not adding enough info but it turns out I added a bit much.
For instance, the input I'm giving is:
$facInputRankLimit = 5;
$facInputRanks[1] = 1; -> $facInputRanks[5] = 5;
But it prints:
1<br><br>2<br><br>3
#Deepkak - The relative code in faction2.html is:
<div class="fpDivisions fpBox">
<span class="header">Ranks</span><br>
<?php
$facRankCount = 0;
while ($facRankCount < $facRankLimit) {
echo $facRanks[$facRankCount].'<br>';
$facRankCount++;
}
?>
</div>
I Hope this will solve the issue
<?php
echo "\nHow many ranks were there (number)?:\n";
$facInputRankLimit = readline();
echo "Please read carefully and supply the ranks in descending order (HIGHEST > LOWEST):\n";
$facInputRankCount = 0;
while ($facInputRankCount < $facInputRankLimit) {
echo "Enter a rank:\n";
$facInputRanks[$facInputRankCount] = readline();
$facInputRankCount++;
}
$array = [];
foreach ($facInputRanks as $facInputRankList) {
$array[] = "'".$facInputRankList."'";
}
$facInputRankString = "[".implode(",",$array)."]";
$facBuildPage = <<<EOT
<?php
\$facRankLimit = '$facInputRankLimit';
\$facRanks = $facInputRankString;
include('faction2.html');
?>
EOT;
echo $facBuildPage;
I've created a workaround for this, incase anyone needs any help in the future. It was as simple as echoing it from the HTML page, rather than creating a loop that runs through both the PHP script and the HTML code.
PHP input code:
echo "\nHow many ranks were there (number)?:\n";
$facInputRankLimit = readline();
echo "Please read carefully and supply the ranks in descending order (HIGHEST > LOWEST):\n";
$facInputRankCount = 0;
while ($facInputRankCount < $facInputRankLimit) {
echo "Enter a rank:\n";
$facInputRanks[$facInputRankCount] = readline();
$facInputRankCount++;
}
$facInputList = implode("<br>", $facInputRanks);
PHP output code:
$facBuildPage = <<<EOT
<?php
\$facRanks = '$facInputList';
include('faction2.html');
?>
EOT;
file_put_contents("test/$facFileName.php", $facBuildPage);
HTML output code:
<div class="fpDivisions fpBox">
<span class="header">Ranks</span><br>
<?php echo $facRanks; ?>
</div>

Simple Snippet to read and show content from file

I am looking for a simple solution to add a Snippet in my index.php file to load and display the content shown in a file from an other Domain.
Plan was to add the Code to the 'Footer' before to show a floating ad on several of my websites.
Sourcesite: http://domainX.tld/floating/floater.txt
Content of file: little bit css for styling of the ad + script snippet for a close button + html to get it into shape.
Target Site gets a simple snippet to show content from txt file as its own content.
I have tried by now
<?php
$StrLessDescription = ("//domainX.tld/floating/floater.txt");
$file_contents = file_get_contents($StrLessDescription);
?>
Site loads but doen't shows anything of my code.
<?php
$handle = fopen("//domainX.tld/floating/floater.txt", "rb");
$delimiter = ",";
while (!feof($handle) ) {
$line = fgets($handle);
$data = explode($delimiter, $line);
foreach($data as $v) {
echo $v;
}
}
fclose($handle);
?>
Site wouldn't even load.
<?php
$f = fopen("//domain.tld/floating/floatr.txt", "r");
// Read line by line until end of file
while(!feof($f)) {
echo fgets($f) . "<br />";
}
fclose($f);
?>
Creates an endless amount of where my Code should be
Other Fails i have deleted already.
Once i had a simple snippet that had done the trick, does one have any idea how to accomplish that again?
This should do the trick:
<?php
echo file_get_contents('//domain.tld/floating/floatr.txt');
Sticking to the straightest way to do it as your intention is and supposing that:
URL you provide for the txt file is correct
you have read access to it
the file has contents to display
your PHP version is (PHP 4 >= 4.3.0, PHP 5, PHP 7) to support
the file_get_contents() function
You are missing in your first approach to echo the contents of your variable $StrLessDescription to send it to output.
<?php
$StrLessDescription = ("//domainX.tld/floating/floater.txt");
$file_contents = file_get_contents($StrLessDescription);
echo $file_contents;
?>
Remember that for large projects you could consider using a framework to achieve the same goal in a more organized way. This is a solution to a quick-and-dirty approach you inquiry.

Include 'some.php' just pasting in PHP code, no correct preprocessing

I was using Apache before setting up my new website. With Apache the whole code worked well so this is something with Nginx. Now I'm using Nginx and there were some difficulties that I got to fix. But now I ran into some trouble.
I created a test.php so you can easily see what's going wrong. In the file there is just:
<?php
include 'graph.php';
?>
In the graph.php there is just some PHP code which usually is need to show some graph stuff. That does not matter. What matters is that this code should never be posted or printed out. When I replace the include statement with just the code that is in the file it works. So why does include or even require_once not work properly for me?
Added graph.php code for you
Alright just added the code of the graph.php here:
<?php
# set header layout
include 'inc/header.php';
# get parameters year/month of calendar
$date_d=0;$date_m=0;$date_y=0;
if (isset($_GET['d'])&&isset($_GET['m'])&&isset($_GET['y'])) {
$date_d = $_GET['d'];
$date_m = $_GET['m'];
$date_y = $_GET['y'];
}
# show diagram of actual statistic overview
echo "<img src='myimage.png' alt='statistic'>";
include 'graph/mycalendar.php';
# set up a date if nothing is set
$actualdate = getdate(time());
if ($month == "")
$month = $actualdate["mon"];
if ($year == "")
$year = $actualdate["year"];
# get parameters year/month of calendar
if (isset($_GET['month'])&&isset($_GET['year'])) {
$month = $_GET['month'];
$year = $_GET['year'];
}
$mycalendar = new MyCalendar;
echo $mycalendar->getMonthView($month, $year);
include 'graph/graph.php';
include 'inc/footer.php';
?>
include 'graph/mycalendar.php';
That file stated with
<?
and with
<?php
it now works.

this PHP code functions in XAMPP but AZURE shows code in browser

<?php
require_once('functions.php');
//display links from xml
$links = simple_load_file('links.xml');
foreach($links as $link){
$name = $link['name'];
$ref = $link->loc;
echo "<a href='".$ref."' class='navigation'>".$name."</a>";
}
//Display login boxes if user hasnt logged in
if((!isset($_SESSION['validUser'])) || ($_SESSION['validUser'] != true))
{
?>
//code for showing boxes.....
In the browser, it displays the folowing ;
loc; echo " ".$name." ";} //Display loginboxes.........all the way to != true)) { ?>
When I test this on XAMPP locally it works as intended, but using Azure it produces this.
It appears as if it's reading the '->' as a closing tag for my php. Anyne encounter something like this?
I never did get to find the exact problem, as the project was abandoned. however, as suggested it is most likely a configuration issue from within Windows Azure.

cant write varibles into php file using file_put_contents PHP

I'm trying to edit a file using the file_put_contents
Everything works fine (the data shows up in the test.php, well most of it) but the variables do not.
heres an example of my problem
code for the file that will perform the file_put_contents:
<?php
file_put_contents("test.php","<?
$title = "title_name_goes_here"
echo $title;
?>");
?>
Code that shows up in the target file (test.php):
<?
= this_is_my_name
echo ;
?>
What should show up in test.php:
<?
$title = "title_name_goes_here"
echo $title;
?>
Also, I'm using Dreamweaver to write the code in and it seems to have problems (code errors) when i insert the quote for the $title's value so its ok if i used $title = title_name_goes_here but it doesn't like $title = "title_name_goes_here". When i say its okay, i mean dreamweaver doesn't show any code errors but it still doesn't do what it should.
any ideas?
Escape the quotes (and the dollar signs):
<?php
file_put_contents("test.php","<?
\$title = \"title_name_goes_here\"
echo \$title;
?>");
?>
If you don't want to see the ugly escapes just use single quotes:
<?php
file_put_contents('test.php",'<?
$title = "title_name_goes_here"
echo $title;
?>');
?>

Categories