How to bold a variable? - php

How can I have text within a php variable be bold??
function show_balance_header ($balance, $currency)
{
(string)$display_output = null;
$display_output = fees_main::display_amount(abs($balance), $currency, true);
return $display_output;
}
I want to bold $balance. Is there an easy way or do I need to edit display_amount?
I've tried doing this:
"<b>".$balance"</b>" but this did not get the correct variable,
thanks in advance!!

Needs to be "<b>" . $balance . "</b>". Concatenate <b> to $balance and concatenate again to </b>
Edit:
Assuming this does the printing/formatting: fees_main::display_amount(abs($balance)
You want fees_main::display_amount('<b>' . abs($balance) . '</b>', $currency, true).
abs($balance) gets the absolute value as a number, and the concatenation (explained above) automatically casts them to strings. The rest of the parameters are passed the same way (unmodified, unbolded).
(string)$display_output = null; is unnecessary

On your element html, example:
<p> text <?php echo "<b>{$balance}</b>"; ?> text</p>

answer is to edit the inner function display_amount using Raeki's suggestion, for future questions, i think you must edit the inner-most function as only editing function show_balance_header did not get the correct variable

Related

Can not get the special charcter from query string value using PHP

I need one help. I need to fetch all data from query string using PHP but some special charcters like (i.e-+,- etc) are not coming. I am explaining my code below.
http://localhost/test/getmethod.php?name=Goro + Gun
Here I need to get the value assign to name using the below code.
<?php
$name=$_GET['name'];
echo $name;
?>
Here I am getting the output like Goro Gun but I need the original value i.e-Goro + Gun .Please help me to resolve this issue.
#subhra try this for this case name=Goro + Gun:
<?php
$nameArr = explode('=', $_SERVER['QUERY_STRING']);
$name = str_replace("%20", " ", $nameArr[1]);
echo $name;
?>
$_SERVER['QUERY_STRING'] - this will return you full query string

Assigning php POST array to Javascript array

I know this may sound similar to some past Q/As, I think mine is slightly different though.. I have a webpage which I want to dynamically load text file information. I upload the text file through an iframe and I want to save this information from php to Javascript. Whenever I try to save this as a regular variable it doesn't work so I have tried to do this by saving this information as a part of the $_POST array under a hidden form named $_POST['hidden_form']. Whenever I try to read the php into Javascript, I keep getting an error "Unexpected token ILLEGAL." I have tried the following two codes:
for($i=0;$i< count($_POST['hidden_form']) ;$i++)
{
echo "saved_form[$i]='" . $_POST['hidden_form'][$i]. "';\n";
}
and
saved_form = <?php echo json_encode($_POST['hidden_form']); ?>;
Assigning a php array into a javascript array
I think the error has to do with the " ' " needed to specify the array but not sure. I have no idea where to go from here so any help would be GREATLY appreciated. If there are better methods to do this please let me know. Thanks in advance!
saved_form = '<?php echo addslashes(json_encode($_POST['hidden_form'])); ?>';
Or
for($i=0;$i< count($_POST['hidden_form']) ;$i++)
{
echo "saved_form[$i]='" . addslashes($_POST['hidden_form'][$i]) . "';\n";
}
Both should work, probably had quotes breaking something?
the best way i have used is,
text/javascript
var saved_form = <?php echo json_encode($_POST['hidden_form']) ?>
Please note there are no Quotes around the php so your saved_form is an Object not a string json string witch would require you to to use var form_object = eval(saved_form)
#Lee might have meant this?
Just a note though i would not use the Raw $_POST pass it to a function that can loop though and addSlashes every value inside the post some thing like
<?php
function arr_addSlashes($array){
$ret = array();
foreach($array as $k => $v){
$ret[$k] = addSlashes($v);
}
return $ret;
}
?>

echo function output inside html

I have something like this in one field of my table(MySql):
$data = '<td>apple</td>';
echo $data;
I select this field and echo it into the page.I want to replace 'apple' word with a php function that return a word.So I thought
$data = '<td>myphp_function('fruit');</td>';
echo $data;
but what I see in the page is exactly the line above and not my function output.
how can I do it?
I am not sure if i could explain my mean clearly...
Edited.
According to your last edit, what you need is the following:
$data = '<td>' . myphp_function('fruit') . '</td>';
echo $data;
This is assuming your myphp_function() will return some kind of value.
If the function echoes the value, it will not work as expected!
You can only execute PHP when you open PHP tags. Other than that, it's just plain text/html.
<td>myphp_function('fruit');</td>
To execute your function you have to open PHP tags:
<td><?php myphp_function('fruit'); ?></td>
you have to insert some sort of placeholder into your text. Like this
<td>[fruit]</td>
and then do a replace before printing it out:
$fruit = 'apple';
$text = str_replace('[fruit]',$fruit,$text);
Of course, for the real life usage there will be more complex solution.
So, you will do yourself enormous favor, if you post here your real task with real data example, not oversimplified and useless abstract question.

Return specific HREF attribute using Xpath query

Having a major brain freeze, I have the following chunk of code:
// Get web address
$domQuery = query_HtmlDocument($html, '//a[#class="productLink"]');
foreach($domQuery as $rtn) {
$web = $rtn->getAttribute('href');
}
Which obviously gets the entire href attribute, however I only want 1 specific attribute within the href. I.e. If the href is: /website/product1234.do?code=1234&version=1.3&somethingelse=blaah
I only want to return the variable for "version", so wish to only return "1.3" in my example. What's most efficient way to do this?
You could use parse_url and parse_str to extract that information.
Bingo! Thanks webdestroya, parse_str is exactly what I am after:
$string="/website/product1234.do?code=1234&version=1.3&somethingelse=blaah";
parse_str($string,$return);
$version = $return['version'];
echo "Version: " . $version;
Prints:
Version: 1.3

removing items from url strings in PHP

I would appreciate some help here:
What I want to do:
remove &itemsperpage=10 from:
http://localhost/thi/search/filter.html?type=featured&page=2&itemsperpage=10
and create a link from it:
http://localhost/thi/search/filter.html?type=featured&page=2&itemsperpage=15
here's what I have come up with so far:
<a href="<?php echo url::site(url::current()) . http_build_query($_GET) // don't know what follows ?>"
the framework functions I'm using are:
url::current() = returns current url in controller/action format
url::site() = returns absolute url i.e http://localhost/site/controller/action
so I have to remove '&itemsperpage' from the resulting string in the http_build_query function
but I am having trouble with character encodings and such! please help!
so here's the problem with character encoding:
$needle = '&itemsperpage';
$querystring = http_build_query($_GET) . '<br/>';
// echo $querystring . '<br/>';
$pos = strpos($querystring, $needle);
$remove = substr($querystring, ((int)$pos));
echo substr(str_replace($remove, '', $querystring), 1); // returns ';'
I can't remove the string '&itemsperpage' from the result of http_build_query which is:
'type=featured&page=2&itemsperpage=10' and functions like strstr outputs nothing
I would just do this:
$array = $_GET;
$array['itemsperpage'] = 15;
Then just use your code, but with the new variable (and the ?):
<a href="<?php echo url::site(url::current()) . '?' . http_build_query($array)">
The HttpQueryString class has several methods for getting, setting, modifying query strings and 'translating' their charsets.
You can achieve the effect you're looking for by removing the itemsperpage element from the $_GET array before building the query string.
unset($_GET['itemsperpage']);
And then just use the code you already wrote:
<a href="<?php echo url::site(url::current()) . http_build_query($_GET); ?>">
EDIT: I misread your post. I thought you only wanted to remove the field / value pair from the GET request. All you have to do is overwrite the value with the value you want:
$_GET['itemsperpage'] = 15;
And then use the code you already wrote.

Categories