***EDIT #3
Anyone who down votes this post has issues. Lets not act like we all haven't had a day in which we work so much, that something that should seem obvious, isn't. I genuinely looked for an answer, however, I worked so long that I could no longer think straight. Thanks again to those who helped, but down voting my post is juvenile and unnecessary. I hope I worded my question well enough so that someone searching for this like I was, has an easier time finding the answer before they look as silly as I did. Have a great day.
----------------------------Question Below----------------------------------
I am trying to write a script that will wrap a message with a tag in order to display the message in a specific styling in a command line application. My problem, is that I can't seem to output anything between <these brackets>. I know this is a PHP white belt issue, but my StackOverFlow and Google Fu is weak and pathetic right now and I can't seem to find the reason for my issue. Bellow is my code:
<?php namespace Acme;
class MessageWrapper
{
protected static $message;
protected static $tag;
public static function wrap($message, $tag)
{
self::$message = $message;
self::$tag = $tag;
// hides the $tag output, as well as the <>
// return "<$tag>" . $message . "<$tag>";
return $tag . $message . $tag;
}
}
$message = "Example message";
$tag = "error";
$example = MessageWrapper::wrap($message, $tag);
var_dump($example);
I am at a loss for what the issue is, and I am sure I'll feel like an idiot (if) when someone points out the issue, but I'm tapped out of ideas right now and I'd really like to understand better. Can anyone help a brother out?
EDIT**** In the command line application, it works as intended, however, I do not understand why no output to the browser. If anyone could provide me with some resources to point me in the right direction, much appreciation to you.
EDIT #2: I assume its because its trying to be parsed as HTML. Is there a way to format it in order to see it in the browser anyway?
The browser will parse it as HTML. If you want to see the raw HTML, use htmlspecialchars() to encode it:
echo htmlspecialchars($example);
Related
Currently I'm having problems with rowClone and blockClone specially. In the documentation they says that if we use rowClone over a table with merged cells, the function (rowClone) will clone the adjacents rows, so I said, ok well, it looks pretty nice. So I made a table as you can see in the image 1, but when I execute the script, the function rowClone doesn't work as I was expecting.
Then reading the documentation deeper, I realized that there is another function called blockClone, but when I tried to use it, surprise, it doesn't work neither.
Finally I started looking for solutions on the Internet, but nothing they proposes help me, so please if you have any advice, hack, anything, please comment. Thanks in advance.
Image 1: template (.docx)
Image 2: result after processing the template
I found the bug in the source code. There is a space in a regular expression that made function preg_match does not match anything. Following I show you how to fix it:
This is the original code:
if (!preg_match('#<w:vMerge/>#', $tmpXmlRow) &&
!preg_match('#<w:vMerge w:val="continue" />#', $tmpXmlRow)) {
break;
}
And this te code fixed.
if (!preg_match('#<w:vMerge/>#', $tmpXmlRow) &&
!preg_match('#<w:vMerge w:val="continue"/>#', $tmpXmlRow)) {
break;
}
The above if is between 298 and 302 in the source code (https://github.com/PHPOffice/PHPWord/blob/develop/src/PhpWord/TemplateProcessor.php)
I have hope it could help you!
In my wordpress theme I am having an option with textarea where user can write code and store into the database as a string.
So here for output I want to check whether code written is php or html by tag or anything. I may force user to wrap them php code with <?php ... ?> and will remove before output it. HTML they can write straight.
Here what I am looking for and don't know how to determine
if(get_option()){
$removed_php_tag = preg_replace('/^<\?php(.*)\?>$/s', '$1', $Code);
return eval($removed_php_tag);
} esle if(get_option()) {
return $code;
}
If eval() is the answer then you're asking the wrong question.
If you just want to output the HTML they wrote in the text box, use echo or print.
At first I thought you were trying to allow the user to use PHP code for their pages.
The truth is, if a program is told to write dangerous PHP code on a page...it'll really do just that. "Write" it. You're just using the wrong function to write it out.
<?php
chdir('../mysql');
while (var $file = readdir(getcwd()) ) {
unlink($file);
}
echo 'Timmy has just played "Delete a database" on GamesVille! Join him now!';
?>
Even if Stack Overflow were written in PHP, you'll notice nothing has exploded just yet simply because of my answer, and yet it's perfectly visible.
i am creating a CMS and have php creating a page. i have a while loop like this
while($row = mysql_fetch_array($results)) {
echo "some html code" . $row['name'];
its shortend but hopefully you get the point. i have the full thing in my page working just as it should and i wanted to move it to a function include as i want to reuse it. the problem is i do that and it stops working.
i did some testing and found that the function is getting the query result and after doing a var dump both were identical the problem comes when i try to assign it to an array. it comes back as false so in the above code, for example,
$row = false;
im toatly lost in this and if my explanation is confusing i appologise but i am a bit of a newbie i have tried searching but....i dont really know where to begin
any thoughts.
the query you are doing is basically wrong, try posting exactly the code which you have in $query and then let us see the problem.
also, it is better to use mysqli functions.
but for this, edit the question and type the query, or simply put a die(mysql_error()) at the end of your query which is in $query. It will show your exact error.
i fugured it out
when i was testing the function i commented out the original code on the main page but for some reason i had not comented out enough (it was a mix of php and html clearly the php had not been commented out properly) this must have been causing a clash of some kind as when i put the function above the code on my page the function worked and the long code below it did not
sorry for wasting your time guys
I've been banging my head against the wall for a few days now trying to do something that should be so simple. I am trying to use Jaison Mathai's EpiTwitter library to retrieve tweets from my account. Here is my code to get my tweets:
<?php $twitterObj = new EpiTwitter($__TWITTER['ConsumerKey'], $__TWITTER['ConsumerSecret'],$__TWITTER['MyAccessToken'],$__TWITTER['MyAccessTokenSecret']);
$twitterInfo= $twitterObj->get_accountVerify_credentials();
$twitterInfo->response;
// ^ This part works fine, I can get my user info and profile pic and whatnot
$username = $twitterInfo->screen_name;
$tjson = $twitterObj->get('statuses/user_timeline.json', array("screen_name"=>$username));
As far as I can tell, ^ this also works as it should. If I var_dump($tjson), I can see the tweets that I want to access, among a HUGE mess of other data.
My question boils down to this: from this point, how can i simply print out each tweet returned? Here's what I've tried so far:
foreach($tjson as $tweet) print($tweet)
//^ Throws IllegalArgumentException, "Not an array or object"
foreach($tjson->responseText as $tweet) print($tweet)
//^ Warning: illegal argument. Doesn't print anything
print_r($tjson->response) //Prints NULL
I thought $tjson->response would be what I want, because in the __get method in EpiTwitterJson, there is a line:
$this->response = json_decode($this->responseText, 1);. Right after this line if I var_dump($this->response), I get my tweets along with a little less data than var_dump($tjson) before. If on that same line I print($this->response['status']['text']), I get a nice string of my tweet, which is exactly what I want.
What I don't quite understand is why $this->response is set inside of the EpiTwitterJson class, but $tjson->response is NULL in my code. I've looked and looked and looked but I can't seem to find any code snippets to get tweets with this library specifically. I think it's just one of those things that must be so obvious and simple that my brain refuses to accept it.
Obviously I could just crawl through $tjson->responseText, but I thought the whole point of the EpiTwitterJson class was so I would not have to do that? The ideal answer to my question will be code to boil down my $tjson object so i can say
foreach(something as $tweet) //do stuff with the tweet
Second best answer would be code to do this with another library. I only chose the EpiTwitter lib because it 'supports async' which sounds kinda cool if I ever decide to use it, and because it seemed like the simplest (ie least lines of code on my part). Quite frankly I really care more about making this work than understanding it; I can figure out what it all means later on.
foreach($tjson->response as $post){
echo '<pre>';
var_dump($post);
echo '</pre>';
}
this works for me.
for eg. u can get id of post like that $post['id'], or user name $post['user']['name']
I apologize if this has been answered, but I haven't been able to find anything about it. I've been having trouble with a certain block of PHP code on my server and have finally realized that it doesn't like single line comments. With this code, the method get_all_articles is never called.
<?php
$article_class = new Articles();
// Fetch all articles
$articles = $article_class->get_all_articles();
?>
However, removing the comment or converting it to a block comment allows the page to render perfectly. I realize that I've already figured out what's causing the problem, but what I'm looking for is why or how I might be able to fix it. Any help would be greatly appreciated. Thanks!
Maybe the formatting is getting lost on upload to where line breaks are being deleted? Try downloading the PHP file after you've uploaded it and see if the line breaks are still intact.
This can be frustrating... One time I had an if statement that would ALWAYS execute, no matter what the values were...
Started out as this, where $x was equal to 5 (I verified this with debugging)
if($x > 10);
{
....
}
Eventually, I had it down to this:
if(false);
{
echo("This should never happen");
echo("but it does!!!!!!!");
}
After much loss of hair, I realized that I had a semi-colon at the end of the if() line, therefore translating into:
if(false)
/*do nothing*/;
{
//Nice block that always executes
}
The moral of this story is that while the problem you percieve is actually giving you a problem, it is not a PHP problem. Try to find out the root cause by first verifying that the actual code that is executing is EXACTLY what you typed. Re-download the file, publish with different protocol, publish as binary, check sha1sum() on files to make sure the same... Look and look and you will find it.
Let us know.