I have the following code that creates the meta-description. I'm getting a 0 as meta-description when trying to add a text after the $info['desc'].
The original code is
function apply_meta($info) {
if (isset($info['desc']) && !empty($info['desc'])) {
define('META_DESC', $info['desc']);
}
What I did is :
function apply_meta($info) {
if (isset($info['desc']) && !empty($info['desc'])) {
define('META_DESC', $info['desc'] + 'my text. Read more about ' + $info['desc'] );
}
Period (.) is used in PHP to concat the strings. Replace your + sign with period like this:
function apply_meta($info) {
if (isset($info['desc']) && !empty($info['desc'])) {
define('META_DESC', $info['desc'] . 'my text. Read more about ' . $info['desc'] );
}
PHP doesn't use + to concatenate strings, it uses a period. Replace your plus signs with periods and it should work.
Related
How to remove everything but allow letter, number and underscore only in my validation for the username? I tried this code but it allows space:
function customUsername($username)
{
if (!preg_match('/^[a-z.,\-]+$/i',$username))
{
$this->form_validation->set_message('customUsername', 'The username field');
}
}
Use the following regex
function customUsername($username)
{
if (!preg_match('/^[a-zA-Z0-9_]+$/i',$username))
{
$this->form_validation->set_message('customUsername', 'The username field');
}
}
You can use ASCII code of every letter, number and underscore.
follow this link for ASCII code http://ascii.cl/ .
jQuery is like this.
$('#keyId').keydown(function(e){
var key = e.charCode || e.keyCode || 0;
return (
key == 8 ||
(key >= 23 && key <= 105)
);
});
So Simple It allow only Alphanumeric and Underscore only
<?php
$str='fdfdf5_';
if(preg_match('/^[a-zA-Z0-9_]+$/',$str))
{
echo "success";
}
else
{
echo "failed";
}
?>
I'm having an issue with my register page, i noticed that people can register with alt codes like this "ªµµª" and i tried to fix it by using preg_replace but when i did that i couldn't register anymore, atleast not with the worldwide alphabet
final public function validName($username)
{
if(strlen($username) <= 25 && ctype_alnum($username))
{
return true;
}
return false;
}
Tried to fix it by replacing it with this
if(strlen($username) <= 25 && preg_match("/[^a-zA-Z0-9]+/", $username))
But i'm obviously doing something wrong...
Apparently, you are confusing two different uses of the caret (^) metacharacter.
Indeed, it may be two things in a regular expression:
It may assert the start of the subject, which is what you probably want.
It may negate the class, which is what you're doing in your code.
Source: http://php.net/manual/en/regexp.reference.meta.php
Here is a modified version of your code, with the caret (^) and dollar ($) signs to assert the start and the end of the strings you're analyzing:
function validName($username)
{
if (strlen($username) <= 25 && preg_match("/^[a-zA-Z0-9]+$/", $username))
{
return true;
}
return false;
}
$names = array(
'Abc1',
'Abc$',
"ªµµª"
);
foreach ($names as $name) {
echo "<br>" . $name . ': ' . (validName($name) ? 'valid' : 'invalid');
}
// -- Returns:
// Abc1: valid
// Abc$: invalid
// ªµµª: invalid
Note that you may reduce the code inside your function to one line:
function validName($username)
{
return strlen($username) <= 25 && preg_match("/^[a-zA-Z0-9]+$/", $username);
}
So if the last function ended up working. And I followed all of your instructions to the T, why is this function not working properly? I've looked over it for an hour, tried rewrite it over and over again and all I get is a 0 or no return.
function marketing () {
$newsold = $_POST['newsold'];
$usedsold = $_POST['usedsold'];
$carsSold = $newsold + $usedsold;
$AdSpend = $carsSold * 275;
echo "You shoud spend roughly $AdSpend per year on advertising";
}
marketing();
You echo the values inside the function:
not:
echo "$autoProfit";
but,
<?php
function autoProfits () {
$usedprofit = 1527;
$newprofit = 800;
$newsold = $_POST['newsold'];
$usedsold = $_POST['usedsold'];
$uprofit = $usedsold * $profitused;
$nprofit = $newsold * $profitnew;
$autoProfit = $uprofit + $nprofit;
}
autoProfits();
?>
Take close att with the curly brace where to be placed.
Several things with your code:
1) By default, a form will post with GET and not POST. So either change your PHP variables to $_GET OR change your form's method to $_POST. I prefer to change the method.
<form action="calc.php" method="POST">
2) You're missing a curly brace on your function:
function makeProfit () {
if ($profit >=0) {
echo "Your company is making a profit.";
} else {
echo "Your company is not making a profit.";
}
}
3) In your function adSpend(), you should invert the line for $carsSold.
4) You have used upper and lower-case characters interchangeably in your variable names ($usedSold vs $usedsold). PHP variables are case-sensitive.
5) The "+" operator when used to combine a string and integer may work, but it would be better not to put integers in quotes.
5b) Using a comma will cause PHP to not recognize your variable as a number, so use $profitUsed = 1527; instead of $profitUsed = "1,527";
6) Your variables at the top of a PHP file are not GLOBAL. You'll either need to convert them to global variables, or (I prefer) send them as parameters to your function. An example of the corrected adSpend():
function adSpend ($newSold = 0, $usedSold = 0) {
$adSpendPerCar = 275;
$carsSold = $newSold + $usedSold;
$adSpend = $carsSold * $adSpendPerCar;
echo $AdSpend;
}
adSpend($newSold, $usedSold);
7) Finally, when you expect an integer from user input, you'd be best to verify that you have an integer. There are a lot of ways to do this, one simple method is to do something like this:
$newSold = intval($_POST['newsold']);
$usedSold = intval($_POST['usedsold']);
Edit Change your variables $profitused for $usedprofit and $profitnew for $newprofit
function autoProfits () {
$usedprofit = 1527;
$newprofit = 800;
$newsold = $_POST['newsold'];
$usedsold = $_POST['usedsold'];
$uprofit = $usedsold * $usedprofit;
$nprofit = $newsold * $newprofit;
$autoProfit = $uprofit + $nprofit;
echo $autoProfit;
}
function makeProfit () {
if ($profit >=0) {
echo "Your company is making a profit.";
} else {
echo "Your company is not making a profit.";
}
}
This function is missing the last '}' (curly bracket)
But you should also be sure when calling adSpend(), that the variables you're trying to access is set, else you won't get anything out of it.
Since you've made that setup, then you should be calling the functions right after you've set all the variables, for anything to work.
You're using undefined variables on the products. You defined $usedprofit and $newprofit but you're multiplying $profitused and $profitnew. Since they're not defined, PHP assumes they're 0.
Basically, I've been trying to make a simple Wordpress widget that displays a QR code with the URL of the current page. I'm using a modififed version of the simple text widget that parses PHP too.
function the_qrcode($permalink = '', $title = '') {
if($permalink && $title == '') {
$permalink = 'http://eternityofgamers.com/forums';
$title = 'Forums';
}
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=' .$permalink. '" alt="QR: ' .$title. '"/>;
}
Can someone tell me what's wrong with this? I get a 500 error when I add it to functions.php.
You will need to use the urlencode() function. Generally as a rule of thumb all querystring values should be url encoded.
function the_qrcode( $permalink = '' ) {
if($permalink == '') {
$permalink = 'http://eternityofgamers.com/forums';
}
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data='.urlencode($permalink);
}
Now you can create your QR code with:
the_qrcode(the_permalink());
Also, you had a very bad missing equals sign. It is very important to understand the difference between = and ==. If you don't, no matter the context = and == mean two different things. = assigns the right hand side to the left. == returns true or false whether the left and right hand side are loosely equal (loosely because casting will be used if the sides are not of the same type).
Look at this example (Codepad demo):
$a = 5;
$b = 10;
if($a = 6) {
echo "This always appears because when you assign a truthy (all non-zero numbers are true) to a variable, true is returned.\n";
echo "Also a should now equal six instead of five: " . $a . "\n";
}
if($b == 10) {
echo "This will work as expected because == is a comparison not an assignment.\n";
echo "And b should still be 10: " . $b;
}
Try with:
<?php
function the_permalink( $permalink ) {
if ($permalink == '') {
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=http://eternityofgamers.com/forums" alt="QR Code">';
} else {
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data='.$permalink;
}
}
?>
(I've corrected a bunch of syntax errors)
I have a database table that stores various type of operators and values that make expressions like
if(mydBVal1 mydBExpression mydBval2)
{
// do something....!
}
Here is my code thats shows and example of what I want to say and the help I require
e.g:
$data['myValue'] = 100;
$data['operator'] = "<";
$data['comparison_value'] = 150
if( $data['myValue'] . $data['operator'] . $data['comparison_value'] )
{
///do something......
}
I want that if condition to be read as if(100 < 150){}, but the if condition expression is not working properly!
any one here know how I can make it work?
I think you want to use the eval() function.
Be very careful about sanitising the data from the database before evaling it though as you could allow users to execute PHP code that you don't want them to.
$data['myValue']=100;
$data['operator']="<";
$data['comparison_value']= 150;
$eval = sprintf("return(%d %s %d);", $data['myValue'], $data['operator'], $data['comparison_value']);
if(eval($eval))
{
Also you can take a look into php assert
php.net/assert
<?php
var_dump(assert("1 == 1"));
var_dump(assert("1 === null"));
?>
Sample code I used related to my project:
$assert_statement =
(($typecriteria != 'IS_NULL' || $typecriteria != 'NOT_NULL' ) ? "'".$value."'" : '' )
. " " . $typecriteria . " '" . $criteriavalue."'";
// Active assert and make it quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
if ( $debug >= 1 ) {
print __METHOD__." assert debug ".$assert_statement."<br>";
var_dump(assert( $assert_statement ));
}
if (assert( $assert_statement ) === true )
{
return true;
}