I´m using strpos two timer. In the first if/else it works well but in the second it doesn´t work. This is my code:
if (strpos($word, "mono") == true) {
$type = "Monobloc";
} else {
$type = "Articulated";
}
if ($word, "galva") == true) {
$coating = "Galvanized Rod";
} elseif (strpos($word, "epoxi") == true) {
$coating = "EPOXI 100%";
} elseif ($word, "electro") == true) {
$coating = "Electrozinced";
}
Example:
If the variable word has the value "galva-mono" $type should be "Monobloc" and $coating should be "Galvanized Rod". The problem is that it is assigning well the $type but in the coating it doen´t enter in the if clause.
As stated in the official documentation:
Warning
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE. Please read the section on
Booleans for more information. Use the === operator for testing the
return value of this function.
You are checking the result with == true instead of !== false.
So, try this code:
if (strpos($word, "mono") !== false) {
$type = "Monobloc";
} else {
$type = "Articulated";
}
if (strpos($word, "galva") !== false) {
$coating = "Galvanized Rod";
} elseif (strpos($word, "epoxi") !== false) {
$coating = "EPOXI 100%";
} elseif (strpos($word, "electro") !== false) {
$coating = "Electrozinced";
}
Related
PHP script used:
$product=Mage::getModel('core/store');
$product->load($order->getStoreId());
$value=$product->getWebsiteId();
return $value;
if (strpos($value, '1') !== false) {
return "34501";
}
else if (strpos($value, '3') !== false) {
return "34502";
}
else if (strpos($value, '2') !== false) {
return "34503";
}
Example of expected result:
34501
I tried many times but I did't found anything can you help me out this is my first task
You need to optimize your code. Please check below if it could be helpful for you. And also, check the conditions you have used; it might be the case the product could not be found with the passing id you have used. Could you handle this try-and-catch statement?
Better to use a switch case statement.
$product=Mage::getModel('core/store');
$product->load($order->getStoreId());
$value=$product->getWebsiteId();
try {
if (strpos($value, '1') !== false) {
return "34501";
}
else if (strpos($value, '3') !== false) {
return "34502";
}
else if (strpos($value, '2') !== false) {
return "34503";
}
else {
throw new Exception('Value Not Found');
}
}
catch(Exception $e) {
echo $e->getMessage(), "\n";
}
OR
$product=Mage::getModel('core/store');
$product->load($order->getStoreId());
$value=$product->getWebsiteId();
if (strpos($value, '1') !== false) {
return "34501";
}
else if (strpos($value, '3') !== false) {
return "34502";
}
else if (strpos($value, '2') !== false) {
return "34503";
}
else {
return 'Value not found';
}
Thanks
I have an export to excel button which downloads excel file.
However when i click it shows me error as Trying to access array offset on value of type int
My code is this:
public static function dataTypeForValue($pValue = null)
{
// Match the value against a few data types
if ($pValue === null) {
return PHPExcel_Cell_DataType::TYPE_NULL;
} elseif ($pValue === '') {
return PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ($pValue instanceof PHPExcel_RichText) {
return PHPExcel_Cell_DataType::TYPE_INLINE;
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) { //error comes in this line
return PHPExcel_Cell_DataType::TYPE_FORMULA;
} elseif (is_bool($pValue)) {
return PHPExcel_Cell_DataType::TYPE_BOOL;
} elseif (is_float($pValue) || is_int($pValue)) {
return PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
$tValue = ltrim($pValue, '+-');
if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
return PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
return PHPExcel_Cell_DataType::TYPE_STRING;
}
return PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) {
return PHPExcel_Cell_DataType::TYPE_ERROR;
}
return PHPExcel_Cell_DataType::TYPE_STRING;
}
}
what can be the solution for this..?
In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
(!is_int($pValue) && $pValue[0] === '=' && strlen($pValue) > 1) should work. $pValue[0] will not work with integers so check if it is an int or not before continuing.
I update some files in library and It's work fine for me at php 7.4
Please check updated code library :
Google Drive LInk
I've been searching and I've tried multiple ways with no luck, What's happening in my code looks for a link. It will then determine what type of link it is and adjust it to suit my needs. Once adjusted I need it to go into an Array with the end goal of adding that Array to my database. print_r($items) will be where ill be adding the array to the Database if you know how do do this also I will not say no to the help :D I am guessing it the same as adding a variable to the database.
Here is my code so far :)
//Lets look for links
$urlcomp = array();
$items = array();
foreach($html->find('a') as $element){
if( strpos( $element->href, "http" ) !== false) {
$urlcomp[] = $element->href;
// Look for gov websites
if ((strpos($urlcomp, 'gov') !== false) || (strpos($urlcomp, 'police') !== false) || (strpos($urlcomp, 'nhs') !== false) || (strpos($urlcomp, 'org') !== false) || (strpos($urlcomp, 'council') !== false)){
} else {
if (in_array($urlcomp, $websiteurlall)) {
}else{
echo "First Batch - " . $urlcomp;
$items[] = array($urlcomp);
echo "</br>";
}
}
}else{
$urlcomp = $websiteurlcomp.$element->href;
// Look for gov websites
if ((strpos($urlcomp, 'gov') !== false) || (strpos($urlcomp, 'police') !== false) || (strpos($urlcomp, 'nhs') !== false) || (strpos($urlcomp, 'org') !== false) || (strpos($urlcomp, 'council') !== false)){
} else {
if (in_array($urlcomp, $websiteurlall)) {
}else{
echo "Second Batch - " . $urlcomp;
$items[] = array($urlcomp);
echo "</br>";
}
}
}
}
print_r($items);
$urlcomp[] = $element->href; should have been $urlcomp = $element->href; that was the only issue :S
This is probably going to have a simple answer, but here is my problem. I am currently writing a weak permissions filesystem, I want the user to not have to do any authentication checks if the given file is empty (this is the $filedir variable). I can successfully check if this file is empty, however, if I try to read anything else (shown by file_get_contents(data.data)), it just simply will not work. There are no errors, but the condition will always evaluate as true. I have been stuck on this forever, and I'm still new to PHP, so hopefully, someone can help me out here!
Thank you in advance!
Karl
<?php
$filedir = substr(getcwd(), 0, strpos(getcwd(), "this")).'this/is/' . $_SESSION['user_name'] . '/a' . '/' . $_POST['dataName'];
if ($_POST['c'] == "true") {
$filedir = substr(getcwd(), 0, strpos(getcwd(), "this")).'this/is/a' . '/' . $_POST['dataName'];
}elseif ($_POST['c'] == "") {
// code...
}else {
$filedir = substr(getcwd(), 0, strpos(getcwd(), "this")).'this/is' . '/' . $_POST['c'] . '/a' . '/' . $_POST['dataName'];
}
**//THIS IS THE FIRST CONDITION THAT, WHEN IMPLEMENTED, CAUSES THE SECOND CONDITION TO ALWAYS EVALUATE TO TRUE FOR SOME REASON**
$pass = false;
if (readfile($filedir) == 0) {
$pass = true;
echo "check";
}else {
echo "pass";
}
if ($_POST['auth'] == "1") {
$prev = getcwd();
chdir(substr(getcwd(), 0, strpos(getcwd(), "this")) . 'this/is/adir');
$cue = file_get_contents("data.data");
// throw new \Exception("Incorrect auth token", 1);
if ($_POST['token'] == $cue) {
$_SESSION['apiauth'] == $_POST['token'];
}elseif (file_get_contents($filedir) == '') {
$_SESSION['apiauth'] == '';
}else {
throw new \Exception("Incorrect auth token", 1);
}
chdir($prev);
}elseif ($_POST['permissions'] == true) {
addLog($fn,'Permissions were changed', 'DATABASE-PERMISSIONS', null, null, 'Target: '. $_POST['dataName'] . 'Change: {Type: '.$_POST['type'].', Usertype: '.$_POST['user'].', Name: '.$_POST['name']);
if ($_POST['revoke'] == true && ($_POST['user'] != 'u' || ($_POST['user'] == 'e' || $_POST['user'] == 'a' || $_POST['user'] == 'm' || $_POST['user'] == null))) {
throw new \Exception("Cannot revoke access without proper format", 1);
}
$prev = getcwd();
chdir(substr(getcwd(), 0, strpos(getcwd(), "this")) . 'this/is/adir');
$cue = file_get_contents("data.data");
**//BELOW THIS IS THE SECOND CONDITION THAT FAILS IF THE FIRST CONDITION IS IMPLEMENTED, AND WORKS FINE IF ITS LEFT OUT**
if ($cue === $_POST['token'] || $cue === $_SESSION['apiauth'] || $pass) {
if ($_POST['type'] == 'r') {
chdir(substr(getcwd(), 0, strpos(getcwd(), "this")) . 'this/is/a/dir/path');
if ($_POST['user'] == 'e' || $_POST['user'] == 'a' || $_POST['user'] == 'm') {
$cue = fopen($_POST['dataName'].".data", "w");
fwrite($cue, '**'.$_POST['user'].'**');
fclose($cue);
}elseif ($_POST['user'] == 'u') {
$d = file_get_contents($_POST['dataName'].".secure");
if ($d == '**a**' || $d == '**e**' || $d == '**m**') {
$cue = fopen($_POST['dataName'].".data", "w");
fwrite($cue, '');
fclose($cue);
}
if ($_POST['revoke'] == true) {
$writein = str_replace($_POST['name']."||","",file_get_contents($_POST['dataName'].".secure"));
$cue = fopen($_POST['dataName'].".data", "w");
fwrite($cue, $writein);
fclose($cue);
}else {
if (strpos(file_get_contents($_POST['dataName'].".secure"), $_POST['name']) !== false) {
// throw new \Exception("User already exists in permission slot", 1);
}else{
$cue = fopen($_POST['dataName'].".data", "a");
fwrite($cue, $_POST['name'].'||');
fclose($cue);
}
}
}else {
throw new \Exception("Invalid parameter.", 1);
}
}
}else {
addLog($fn,'Permission changed was blocked due to incorrect token', 'DATABASE-PERMISSIONS', 'danger', null, 'Target: '. $_POST['dataName'] . 'Change: {Type: '.$_POST['type'].', Usertype: '.$_POST['user'].', Name: '.$_POST['name']);
throw new \Exception("Incorrect auth token", 1);
}
chdir($prev);
}
?>
From the manual
Returns the number of bytes read from the file. If an error occurs,
FALSE is returned and unless the function was called as #readfile(),
an error message is printed.
You make a weak comparison on this line
if (readfile($filedir) == 0) {
}
If the call fails false == 0 will evaluate to true, because the int value will evaluate to false. false == false is true. So use strict comparison operator === and try to figure out why the call fails anyway.
if (readfile($filedir) === 0) {
}
or use, if intended if the call succeded, and returned anything (but also 0)
if (readfile($filedir) !== false) {
}
I want to check whether special characters such as "<" ">" or the double quote itself is found in a string. But my function always return true.
Thank you
This is the code:
$name = "<h1><dfdafdfds";
function correctName($name){
if (strlen($name) < 5 || (strpos($name, "<")===true) ||
(strpos($name, ">")===true) || (strpos($name, "\"")===true)){
return false;
}else{
return true;
}
}
Strpos either returns false or an integer value such as 5. It does NOT return true.
Therefore (strpos($name, "<")===true always returns false.
your code evaluates as:
if (strlen($name) < 5 || false) ||
(false) || (false)){
return false;
}else{
return true;
}
You need to use this format:
strpos($name, '<') !== false
so your code should look like:
if (strlen($name) < 5 || strpos($name, "<") !== false || strpos($name, ">") !== false || strpos($name, "\"") !== false) {
strpos never returns TRUE. It might return FALSE. Solution: change your comparisons to !== FALSE