Hi I have a PHP variable called Keyword, what I am trying to find out is if there is any content in the variable.
if (isset($keyword)) {
Using the above code it returns values that are blank.
Use empty():
if (isset($keyword) && !empty($keyword)) {
Use this:
if (!empty($keyword)) {
try
trim($keyword);
if (isset($keyword) && !empty($keyword)) {
// Code here
}
Edit:
This fixed your problem because you had trailing whitespace in your variable. even though it appeared empty it still had a space or carriage return.
Related
I have zipcodes being outputted, coming from user inputted values. Looks like it is outputting zero-width-space \u200b sometimes at the beginning of the strings.
What is the best way to replace these from within php before echoing the variable?
I use this function to trim unicode spaces - this should work in your case too.
function trimUnicode($str) {
return preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u','',$str);
}
Ok, seems as though this was coming from the actual string being echo'd by PHP, so I did the following to the string:
$zipcode = trim(utf8_decode($zipcode), '?');
All seems fine now!
PHP newbie here
Can anyone please tell me what is wrong with the below syntax. I have a maximum of 4 files - $created_page1, $created_page2 each with a corresponding page title etc and would like to process these in a loop. However PHP throws a wobbly every time I try to concatenate the string and loop number - specifically $created_page.$num_pages doesn't result in sending $created_page1 or $created_page2 to the function, instead it just converts the string and number to an integer. Very basic I am sure but I would be very grateful for any help or a nicer solution that I can easily understand. Thanks in advance!
$addit_pages == 4;
for ($num_pages=1;$num_pages<=$addit_pages ;$num_pages++) {
replaceFileContent ($dir,$created_page.$num_pages,"*page_title*",$page_title.$num_pages);
//replaceFileContent ($dir,$created_page2,"*page_title*",$page_title2);
//replaceFileContent ($dir,$created_page1,"*page_title*",$page_title3);
//replaceFileContent ($dir,$created_page3,"*page_title*",$page_title4);
}
Your code to get the variable name should be:
${'created_page'.$num_pages}
This is because you have to evaluate the string inside the braces before you attempt to access the variable.
Your previous code was trying to access the variables $created_page and $num_pages, and simply concatenate their values into a string.
Of course, the same goes for the page_title variable
${'page_title'.$num_pages}
you could try this:
$addit_pages == 4;
for ($num_pages=1;$num_pages<=$addit_pages ;$num_pages++) {
replaceFileContent ($dir,$created_page.strval($num_pages),"*page_title*",$page_title.strval($num_pages));
//replaceFileContent ($dir,$created_page2,"*page_title*",$page_title2);
//replaceFileContent ($dir,$created_page1,"*page_title*",$page_title3);
//replaceFileContent ($dir,$created_page3,"*page_title*",$page_title4);
}
the PHP strval function makes any integer into a string
I think what you are asking is you want the variables $created_page1; $created_page2, $created_page3 but php is probably throwing a notice that $created_page doesn't exist.
You need to use variable variables (is this what they're called?)
$addit_pages == 4;
for ($num_pages=1;$num_pages<=$addit_pages ;$num_pages++) {
$createdVar = 'created_page'.$num_pages;
$titleVar = 'page_title'.$num_pages;
replaceFileContent ($dir,$$createdVar,"*page_title*",$$titleVar);
}
When you use $$ this first evaluates the variable $createdVar turns that into created_page1 and then evaluates created_page1 as if you had typed in $created_page1
I have CKEditor embedded in my page. I need to prevent plain whitespaces and breaklines that doesn't come with any characters. There must be at least one actual visible character.
The following answer is totally not consistent, sometimes it works fine and sometimes it does nothing, it allows whitespaces:
if(!empty($_POST['rtxt_article']))
{
if (trim(strip_tags($_POST['rtxt_article']))) {
// do something
}
else
{
//ops! please fill in data
}
}
else
{
//ops! please fill in data
}
I also tried this:
$plainText = strip_tags($_POST['rtxt_offer']);
$isNotEmpty = trim($plainText);
if($isNotEmpty)
{
//do something
}
When the above snippet doesn't have effect anymore, i put ! sign and the snippet works again. After a while, the snippet doesn't work until i remove ! and vice versa. Totally inconsistent. This is how i put !:
if(!$isNotEmpty) ...
if (!trim(strip_tags($_POST['rtxt_article']))) ...
Any idea? Any other solution?
Give this a shot. It will first check to see if their is actually input but using the empty() function. With adding the ! to empty() what happens is that the if statement is being asked if $_POST['rtxt_article'] is NOT empty, meaning that there is at least once character in it.
if (!empty($_POST['rtxt_article']) && trim(strip_tags($_POST['rtxt_article']))) {
// do something
}
If for some reason it is still being passed with the new line character, then you could scrub the $_POST var first.
edited:
$var = trim($_POST['rtxt_article']);
if (!empty($var)) {
// do something
}
The CMS I'm using has a plugin that pulls a series of pages based on how you've tagged those pages. Everything is working fine, but I need to display the number of pages returned after a user sends a query.
The variable that the number of records is stored in is a string. The small script I'm writing tries to check if this string is blank, and if so echo nothing, but if it's not blank echo the number of pages returned.
<?php
if ($count !== ''){
echo "text";
}
?>
However, whenever it's passed when the string is supposed to be empty it treats it as if it is not. I'm not sure what I'm doing wrong or what the string contains that it's not empty.
I found a resource about converting strings to integers but it set it to 0. Thanks for any help.
$count will never be equal to ' ' if you trim it before.
It cannot be equal to ' ' after trim, you should check if it is equal to an empty string and not a string with a white space. (trim deletes white spaces)
trim — Strip whitespace (or other characters) from the beginning and end of a string
http://php.net/manual/en/function.trim.php
trim removes all spaces, so $count == ' ' will always be false if you trimmed first. The easiest change to your code would be to replace the ' ' with ''. Or you could just do this: echo trim($count) === '' ? '' : "text";
trim() is going to remove all white-space characters from the beginning and end of the string. Unless there is content in the middle, you'll likely end up with a completely empty string. If you're testing for this, try checking empty() or is_null().
if (empty($string)) {
echo "String is empty.";
}
I find it's better using built-in functions instead of hard-coding a =='' comparison.
Use var_dump() to check the type and the content of the variable.
Check the manual
Your code seems wrong; you've got "!==", where I'd swear you need to have "!=" (only one equals sign). I'm not sure what "!==" would evaluate to, but I'd bet it's not what you want.
It seems from the comments that your "blank" string is not blank, but instead has something odd in it. Assuming that it's not too odd I'd just try this:
$count = intval($count);
if ($count) {
echo 'text';
}
Though this assumes that the $count actually looks like a number to intval when it's got a number in it -- we may need to take a look at this "string" you're getting back in more detail to figure out what's really in it.
Hopefully you've already taken the advice given by others and looked at the resulting page source after doing a var_dump() -- otherwise I'd guess that $count has XML in it, or something else that won't render well if you dump it to an HTML page and view the page rather than the source...
If nothing else, you could really try brute-forcing it:
$count = intval(preg_replace('/[^\d]/', '', $count));
if ($count) {
echo 'text';
}
...but really it'd be better to work out what this odd plug-in is giving you, and why. Which CMS and plugin is it? Are there some docs available for this thing that's returning $count to you? Do you have the plugin source?
Hey guys, it's kind of hard to explain but basically I want to detect if any variables have been set through the URL. So with my IF statement all of the following should return true:
http://domain.com/index.php?m=100
http://domain.com/index.php?q=harhar
http://domain.com/index.php?variable=poo&crazy=yes
and all the following return false:
http://domain.com/index.php
http://domain.com/test.php
http://domain.com/no_variables.php
Any ideas?
I would test for QUERY_STRING:
if (!empty($_SERVER["QUERY_STRING"]))
should in effect be no different from checking $_GET, though - either way is fine.
if( !empty( $_GET ) ) {
//GET variables have been set
}
(count($_GET) > 0)
If you want to do it with the exception of (a) variable(s), use this if statement before it checks it:
if (!isset($_GET['getvariable'])) {
if (!empty($_SERVER["QUERY_STRING"])) {
echo "do something";
}
}
If you mean taking a string and checking if it has a query string, you can use parse_url.
If you mean checking if the current request has a query string, you can just check the length of $_SERVER['QUERY_STRING'].
If you mean to get a count of the number of variables parsed from the query string, you can do count($_GET);
isset($_GET['m'])
or if anything, I believe count($_GET) might work.