PHP : Sum up the result of some "echo do_shortcode" - php

I'v been searching all the website / web with to achieve that, with no luck. I'm not a developper at all also, so the few information that I could get, didn't help me either.
The situation : I have some Wordpress plugins, that allows me to generate some numbers with shortcode. For example I have [shortcode1] from one plugin, and [shortcode2] from another, each of them returning a dynamic number.
I'm using PHP in my pages, to display that number, by a simple "php echo do_shortcode('[shortcode1]');" , same for shortcode 2, and it works well.
I'm trying to sum up the value that is returning from these 2 shortocdes. Lets say shortcode1 = 10 and shortcode2 = 20, I need to display "30".
I tried sending the "do_shortcodes" into arguments, then using "sum", or use "+", but none of this worked, and actually it makes sense because when doing so, I am attributing the shortcode to this argument, not the actual result of it. Knowing that, I then tried to apply "echo do_shortcode" to the argument instead, to save the result instead of the actual shortcode. But apparently it's not correct to do it, or I don't have the way to do it.
I hope I could explained properly, any idea of how I could achieve this ? (Wishing there could be an answer as simple as 1+1=2 :) )
Thanks in advance

You can ensure numbers only like this, but do_shortcode() is the wrong approach for what you want to do in most cases. The following is not efficient in terms of programming but will do what you want. You could just generate a variable and use that but i suppose you have your reasons. Anyway...
$a = (int) preg_replace('/[^0-9]/','',do_shortcode('[whatever]'));
$b = (int) preg_replace('/[^0-9]/','',do_shortcode('[whateverb]'));
$c= $a + $b;
echo $c;

Related

Using Eval as a variable?

I know that a lot would be against using eval, but I do know the
dangers. Everything I use eval for will be for my own use and no user
input will be used.
I'm trying to grab some content by using $Content = "Content here"; But I also need to use eval($content); as the value of content will be containing php code that i'll need to execute. I also want to be able to use eval as a value. So for example using $Example = eval($content);
As I said, I know it can be dangerous, But its a risk im going to take to handle it. I'd just like my question to be answered. Thank you in advance
Here is an example of use of eval that returns a value:
php > $a = 'return 3+3;';
php > $b = eval($a);
php > echo $b;
6
As written in a comment, read always the documentation. In this case, the page about eval.

PHP Generate Random String In Echo

I'm looking for a bit of assistance in generating a random string of numbers that is 4 digits long. To better explain my situation, here is a snippet of PHP code where I need the random numbers inserted (for those wondering about the $ssh->exec I'm using phpseclib).
echo $ssh->exec("touch $username-$port.txt");
The above $port variable is where I'd like PHP to generate a random 4 digit number (preferably between 5000-5999).
After looking at the documentation on W3Schools, it looks like they recommend using the following to generate a random number between defined limits, like so:
echo(rand(10,100));
This works standalone, but the problem with that is it requires an echo and I'm already performing an echo $ssh->exect with my above PHP code. So I'm not quite sure how I would implement an "echo inside an echo" if that makes sense.
If anyone would be willing to help me out with this, it would be greatly appreciated.
You can use a pre-defined variable such as:
$port = rand(5000,5999);
that will then populate through your exec provided you keep it encapsulated inside double quotes. Otherwise, you will need to concatenate.

PHP strcmp question

I have been writing a webcrawler program, and I am attempting to compare a previous url (for the last site visited) with a current url (the current or next site to visit). To do this I am using a strcmp function such as this:
array_push($currentsite, $source);
if (strcmp($currentsite[2], $currentsite[3])==0){
echo "redundancy";
crawlWebsite($originalsource);
}
where current site is an array of the previous sites and the current site. I am looping through new sites each time with recursion in the larger program.
However, every time I run a strcmp on the current site and the new site, even when the urls are identical, I get a result of -1. Does anyone know why this might consistently be happening?
Thanks.
even when the urls are identical,
If the two input strings are identical, strcmp returns 0, so your input strings are not identical. Check the contents of $currentsite.
BTW strcmp($a, $b) == 0 can be efficiently rewritten as $a == $b.
Probably the site you are testing contains something that makes it unique, like the current time or a hidden ID to save your session or something like this.
Anyway that will result in strcmp to not return 0. It would be bettor to have a function that gives you a percentage of equality so you can define a level above which you consider two sites as identical.

convert $var of function to string?

My goal just debug
function dbg($var){
echo "you have passed $var";
}
call dbg($test)
output:
you have passed test
call dbg("var")
output:
you have passed "var"
In php .anyone could help me to do that?
Try this if $var is a global variable:
function dbg($var){
echo "you have passed {$GLOBALS[$var]}";
}
Well, the second case is fairly straightforward - you're passing a string and you want to display the string. No worries.
But for the first case, I'm afraid the answer is: No you can't.
Once inside the function, PHP doesn't know anything about the variable that was passed into it other than the value.
I can't really see that it would be of much value though. It would be trivial to change your code to pass in a name and a value -- ie something like this:
function dbg($name,$value) {
print "You passed $name, and the value was $value";
}
dbg('test',$test);
That's not really all that great either though -- you may as well just use print_r() and friends.
If you really want more powerful debugging tools, you should look into xDebug. It's a proper debugging tool for PHP, which allows you to step through the code line-by-line, and see the contents of variables at any point during the program run (among many other good features). It also integrates nicely with several popular IDEs.

What is the best way to capture data returned from a function in PHP?

I am new to programming and learning with Wordpress.
the_title(); //outputs the title of the page
I want to capture the title of the page into a string variable so I can manipulate it with strtolower and str_replace functions.
The only way I have gotten it to work is with output buffering.
ob_start();
the_title();
$result = ob_get_clean();
echo str_replace(" ","-",strtolower($result));
/*there has got to be an easier way....
i dont fully understand why */
str_replace(" ","-",strtolower(the_title()));
What am I doing wrong?
If what you really are looking for is the wp_title function, the 2nd argument it takes is a boolean on whether or not it should display it or return it. Pass it false so it will return it to the var, then you can do this:
$mytitle = wp_title(null, false);
Otherwise, your only option is to find the function you're looking for and modify the source code.
There is no easier way. Your function does not return the string, it prints it, therefore you will have to use output buffering if you want to capture the output.
It's the difference between f1() and f2() in the following example.
// Returns a string, prints nothing.
function f1() {
return "String";
}
// Prints a string, returns nothing.
function f2() {
echo "String";
}
Wordpress is a HORRIBLE app to learn how to program from. It uses these global functions that "just work" but they do very specific tasks "inside 'The Loop'". As I say, this is a horrible example of what good code should be.
Thankfully (for you) there are other functions that just return the part you're looking for. Rather than me just writing what you need, you can read a full listing here. Take care that you note down which must be within the mythical Loop and which you can use anywhere.
As it happens there are even more ways to get the title, but I was really imagining for this example you would do something like:
$this_post = get_post($post); // I *think* $post is the post ID inside the loop though I could be wrong
echo $this_post->post_title;
But as another poster (correctly) says you can use a fairly simple wp_title() function to grab the current loop title.
This brings me back to perhaps wanting to explain why learning programming from Wordpress is a bad idea. They have so many damned way of doing the same damned thing that it's almost impossible to keep on top of things.
A blog is a really simple set of data (even moreso in WP's case because it isn't fully normalised) but rather than just having one way to output a title <?php echo $post->title; ?> you have umpteen ways, all doing subtly different things.
If you really want to learn how to program (instead of hacking your way around the crap that is the WP internals), creating a simple blog engine is fairly quick and fun... It's certainly how a lot of people get into a new language or framework.
And if you really want to have fun, have a look at Django.
Enough of the Wordpress rant. If you're fighting something like this in the future that doesn't have 100 ways of doing it, I really wouldn't recommend output-buffer-capturing. It uses up a whole buttload of resources for something relatively simple.
The easiest way can be as simple as just taking the source for the original function, sticking it in a new function and replacing the echo with return.
Just note there may be some database connectivity to handle that returning prematurely may break... So if the echo isn't the last statement, instead of returning right there, store the string as a variable and return at the end of the function.
just figured Id share my final solution with you guys.
This was to give my body tags unique id's in wordpress.*/
$title =wp_title(null,false);
echo strtolower(str_replace(' ','-',ltrim($title)));
//without the ltrim() 2 dashes are created before the title.
Almost every 'the_*' function in Wordpress has a 'get_the_*' counterpart. So, you just have to use
echo str_replace(" ","-",get_the_title());
And it's going to work like a charm. there's also get_the_excerpt(), get_the_content() and the_permalink() which somehow breaks the naming convention (God knows how many times I've written "get_the_permalink()" and got frustrated on why it didn't work)
Cheers!

Categories