I'm using the following function to open a file:
function example() {
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
echo $foo2;
}
}
Here's the code where it's called:
<?php
include($_SERVER['DOCUMENT_ROOT']."/class_lib.php");
$page = new Page();
function example() {
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
echo $foo2;
}
}
$page->meta = array
(
'title' => 'snip',
'description' => 'snip'
);
$page->content = "
snipsnipsnipsnipsnipsnip
<div id=\"foo\">
<pre>
".example()."
</pre>
</div>
<br/>snipsnip
";
$page->Display();
?>
For some reason, even though the function is called within the pre element, it appears at the start of the page (the file is output, then the html is loaded). Same thing happens when I use include(). I must overlooking something obvious... any ideas?
Here's the class_lib.php if it's needed: http://pastebin.com/7euqEWNq
You use echo when reading your file, so it's directly printed out.
You can used output buffering : ob_start
Or simply get the file content and use it instead or you function call : file_get_contents
What you should to is let the example method return a value as the result and than echo that at the position you are calling it from. Like so:
function example() {
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
return $foo2;
}
}
$exampleData = example();
$page->content = "snipsnipsnipsnipsnipsnip
<div id=\"foo\">
<pre>".$exampleData."</pre>
</div>
<br/>snipsnip";
I guess that's because your echo sentence, the first thing to be sent to the browser is the echo sentence output, so it shows first no matter where you place it.
You should return not echo, something like this:
function example() {
$output = '';
$foo = fopen('file.txt', 'r');
while (!feof($foo)) {
$foo2 = fgets($foo);
$output .= $foo2;
}
return $output;
}
Related
I've written some code in php to scrape some preferable links out of the main page of wikipedia. When I execute my script, the links are coming through accordingly.
However, at this point I've defined two functions within my script in order to learn how to pass links from one function to another. Now, my goal is to print the links in the latter function but it only prints the first link and nothing else.
If I use only this function fetch_wiki_links(), I can get several links but when i try to print the same within get_links_in_ano_func() then it prints the first link only.
How can I get them all even when I use the second function?
This is what I've written so far:
include("simple_html_dom.php");
$prefix = "https://en.wikipedia.org";
function fetch_wiki_links($prefix)
{
$weblink = "https://en.wikipedia.org/wiki/Main_Page";
$htmldoc = file_get_html($weblink);
foreach ($htmldoc->find("a[href^='/wiki/']") as $a) {
$links = $a->href . '<br>';
$absolute_links = $prefix . $links;
return $absolute_links;
}
}
function get_links_in_ano_func($absolute_links)
{
echo $absolute_links;
}
$items = fetch_wiki_links($prefix);
get_links_in_ano_func($items);
Your function returned the value at the very first iteration. You will need something like this:
function fetch_wiki_links($prefix)
{
$weblink = "https://en.wikipedia.org/wiki/Main_Page";
$htmldoc = file_get_html($weblink);
$absolute_links = array();
foreach ($htmldoc->find("a[href^='/wiki/']") as $a) {
$links = $a->href . '<br>';
$absolute_links []= $prefix . $links;
}
return implode("\n", $absolute_links);
}
I have a function for dumping variables on the screen and what I'd like to do is to show the name of the variable next to the value of the variable, so it would output something like this:
function my_function($var) {
return '<pre>' . var_dump($var) . '</pre>';
}
$myVar = 'This is a variable';
echo my_function($var); // outputs on screen: myVar has value: This is a variable
$anotherVar = 'Something else';
echo my_function($anotherVar); // outputs on screen: anotherVar has value: Something else
How can I do this ?
PHP offers no simple way of doing this. The PHP developers never saw any reason why this should be neccesary.
You can however use a couple of workarounds found here:
Is there a way to get the name of a variable? PHP - Reflection and here: How to get a variable name as a string in PHP?
Probably, debug_backtrace() function in such case will be the most effective:
function my_function($var) {
$caller = debug_backtrace()[0];
$file = $caller['file'];
$lineno = $caller['line'];
$fp = fopen($file, 'r');
$line = "";
for ($i = 0; $i < $lineno; $i++) {
$line = fgets($fp);
}
$regex = '/'.__FUNCTION__.'\(([^)]*)\)/';
preg_match($regex, $line, $matches);
echo '<pre>'. $matches[1]. ": $var</pre>";
}
$anotherVar = 'Something else';
my_function($anotherVar);
// output: $anotherVar: Something else
I'm new to PHP and I come from Objective-C. I need to create a plugin for WP that returns an HTML table where each row is populated by data from JSON. In essence, as an example, I need to replace echo with return:
$jsonurl = "http://xxxx/club/api/xxxx/category/";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
//print_r ($json_output);
echo "<table>";
foreach ( $json_output->result as $result )
{
echo "<tr><td>".$result->id."</td><td>".$result->categoryKind."</td><td>".$result->ranking."</td>";
}
echo "</table>" ;
That works! I can see output as expected. But for showing table in WP through Shortcode, I need return and no echo. So how can I replace the echo with return?
I tried:
function foobar_func(){
$html= "<table>";
foreach ( $json_output->result as $result )
{
$html. = "<tr><td>".$result->id."</td><td>".$result->categoryKind."</td><td>".$result->ranking."</td>";
}
$html. = "</table>" ;
return $html;
}
add_shortcode( 'foobar', 'foobar_func' );
without success. Any help is welcome.
UPDATE: Same result (no work). I will exit crazy.
function foobar_func($json_output){
$html= "<table>";
foreach ( $json_output->result as $result )
{
$html. = "<tr><td>".$result->id."</td><td>".$result->categoryKond."</td> <td>".$result->ranking."</td>";
}
$html. = "</table>" ;
return $html;
}
add_shortcode( 'foobar', 'foobar_func' );
please try ob_start() method i think it useful to you.
http://php.net/manual/en/function.ob-start.php
<?php
function callback($buffer)
{
// replace all the apples with oranges
return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
ob_end_flush();
?>
Variable scope is your problem here. $json_output isn't accessible within the function.
Change to the following:
function foobar_func(){
global $json_output;
$html= "<table>";
Alternatively to calling it as a global variable, you can pass it within the function call.
function foobar_func($json_output){
And then when you call the function, use foobar_func($json_output)
After investigating I found the way out. But honestly I can't understand why this code works. Thank you all for putting me on the right path.
Code:
function foobar_func($json_output){
$jsonurl = "http://xxxx/club/api/xxx/category/";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
echo "<table>";
foreach ( $json_output->result as $result )
{
echo "<tr><td>".$result->id."</td><td>".$result->categoryKind."</td><td>".$result->ranking."</td>";
}
echo "</table>" ;
return $html;
}
add_shortcode( 'foobar', 'foobar_func' );
I'm stuck on how to write the test.php page result (after php has run) to a string:
testFunctions.php:
<?php
function htmlify($html, $format){
if ($format == "print"){
$html = str_replace("<", "<", $html);
$html = str_replace(">", ">", $html);
$html = str_replace(" ", " ", $html);
$html = nl2br($html);
return $html;
}
};
$input = <<<HTML
<div style="background color:#959595; width:400px;">
<br>
input <b>text</b>
<br>
</div>
HTML;
function content($input, $mode){
if ($mode =="display"){
return $input;
}
else if ($mode =="source"){
return htmlify($input, "print");
};
};
function pagePrint($page){
$a = array(
'file_get_contents' => array($page),
'htmlify' => array($page, "print")
);
foreach($a as $func=>$args){
$x = call_user_func_array($func, $args);
$page .= $x;
}
return $page;
};
$file = "test.php";
?>
test.php:
<?php include "testFunctions.php"; ?>
<br><hr>here is the rendered html:<hr>
<?php $a = content($input, "display"); echo $a; ?>
<br><hr>here is the source code:<hr>
<?php $a = content($input, "source"); echo $a; ?>
<br><hr>here is the source code of the entire page after the php has been executed:<hr>
<div style="margin-left:40px; background-color:#ebebeb;">
<?php $a = pagePrint($file); echo $a; ?>
</div>
I'd like to keep all the php in the testFunctions.php file, so I can place simple function calls into templates for html emails.
Thanks!
You can use output buffering to capture the output of an included file and assign it to variable:
function pagePrint($page, array $args){
extract($args, EXTR_SKIP);
ob_start();
include $page;
$html = ob_get_clean();
return $html;
}
pagePrint("test.php", array("myvar" => "some value");
And with test.php
<h1><?php echo $myvar; ?></h1>
Would output:
<h1>some value</h1>
This may not be exactly what you're looking for but it seems you want to build an engine of sorts for processing email templates into which you can put php functions? You might check out http://phpsavant.com/ which is a simple template engine that will let you put in php functions directly into a template file as well as basic variable assignment.
I'm not sure what printPage is supposed to be doing but I would re-write it like this just to make it more obvious because the array of function calls is a bit complicated and I think this is all that is really happening:
function pagePrint($page) {
$contents = file_get_contents($page);
return $page . htmlify($contents,'print');
};
and you might consider getting rid of htmlify() function and use either of the built-in functions htmlentities() or htmlspecialchars()
Seems like my original method may not have been the best way of going about it. Instead of posing a new question on the same topic, figured it was better to offer an alternate method and see if it leads to the solution I am after.
testFunctions.php:
$content1 = "WHOA!";
$content2 = "HEY!";
$file = "test.html";
$o = file_get_contents('test.html');
$o = ".$o.";
echo $o;
?>
text.php:
<hr>this should say "WHOA!":<hr>
$content1
<br><hr>this should say "HEY!":<hr>
$content2
I'm basically trying to get $o to return a string of the test.php file, but I want the php variables to be parsed. as if it was read like this:
$o = "
<html>$content1</html>
";
or
$o = <<<HTML
<html>$content1</html>
HTML;
Thanks!
I have a simple regex which checks an entire string for a function declaration. So in this code:
public function Test($name)
{
echo 'In test';
}
It will find the first part:
function Test($name)
{
And it replaces that with a custom piece:
function Test($name)
{
echo 'New piece';
Which eventually makes my code look like this:
public function Test($name)
{
echo 'New piece';
echo 'In test';
}
This all works perfectly fine with this regex:
preg_match_all ( '/function(.*?)\{/s', $source, $matches )
The problem is, is that i want to ignore everything when the regex sees a script tag. So in this case, this source:
public function Test($name) //<--- Match found!
{
echo 'In test';
}
<script type="text/javascript"> //<--- Script tag found, dont do any matches!
$(function() {
function Test()
{
var bla = "In js";
}
});
</script> //<--- Closed tag, start searching for matches again.
public function Test($name) //<--- Match found!
{
echo 'In test';
}
How can i do this in my regex?
As mentioned in the comments:
If your php functions always have a visibility modifier like public you could do:
(?:public|protected|private)\s+function\s+\w+\(.*?\)\s*\{
Otherwise, you could strip the script part first.
Something like:
$text = preg_replace('/<script(?:(?!<\/script>).)*<\/script>/s','',$text);
I don't know python, but I know regex:
Your original regex is not so good, since it matches
// This is a functional comment { isn't it? }
^^^^^^^^...........^
Maybe if you make it more robust it will solve your problem:
^\s*(public|protected|private)\s+function\s+\(.*?\).*?{
This will ensure it is a function declaration for 99% of the cases. There are still some unusual cases where you can fool it.
No amount of regex is going to achieve a decent fail-proof solution.
The right way to do this is with php tokenizer.
<?php
$code = <<<END
<?php
public function Test(\$name) //<--- Match found!
{
echo 'In test';
}
?>
<script type="text/javascript"> //<--- Script tag found, dont do any matches!
$(function() {
function Test()
{
var bla = "In js";
}
});
</script> //<--- Closed tag, start searching for matches again.
<?
public function Bla(\$name) //<--- Match found!
{
echo 'In test';
}
END;
function injectCodeAtFunctionsStart ($originalCode, $code)
{
$tokens = token_get_all ($originalCode);
$newTokenTree = '';
// iterate tokens
for ($i = 0, $total = count($tokens); $i < $total; $i++)
{
$node = $tokens[$i];
$newTokenTree[] = $node;
if (is_array ($node))
{
// function start
if ($node[0] == T_FUNCTION)
{
// walk to first brace
while ($tokens[$i] !== '{') {
$newTokenTree[] = $tokens[$i];
$i++;
}
$i++;
// keep space
$space = $tokens[$i];
$newTokenTree[] = $space;
// add new piece
$newTokenTree[] = $code;
$newTokenTree[] = $space;
}
}
}
// rebuild code from tokens
$content = '';
foreach ($newTokenTree as $node) {
$content .= is_scalar ($node) ? $node : $node[1];
}
return $content;
}
echo injectCodeAtFunctionsStart ($code, 'echo "new piece";');