Uninitialized string offset error in simple for loop php - php

my goal is to convert every space " " in string to "%".
Here is my function:
<?php
$nazov = "dasa sdas da sd";
$buttonNazov = "";
for($i=0;$i<=strlen($nazov);$i++) {
if($nazov[$i] === " ") {
$buttonNazov .= "%"; // Line# 6
} else {
$buttonNazov .= $nazov[$i]; // Line#12
}
}
echo $buttonNazov;
?>
I am getting output but also 2 errors:
( ! ) Notice: Uninitialized string offset: 15 in C:\wamp\www\test.php on line 6
( ! ) Notice: Uninitialized string offset: 15 in C:\wamp\www\test.php on line 12
dasa%sdas%da%sd

from Mark Baker comment: Offset begins at 0, not at 1; so $i<strlen($nazov) and not $i<=strlen($nazov)
below is more better way of writing same
<?php
$nazov = "dasa sdas da sd";
$buttonNazov = "";
$len = strlen($nazov);
for($i=0; $i<$len; $i++) {
if("" === $nazov[$i]) {
$buttonNazov .= "%";
} else {
$buttonNazov .= $nazov[$i];
}
}
echo $buttonNazov;
?>
alternative way if you want to replace space with %
$buttonNazov = str_replace(' ', '%', $nazov);

the last index of a string $nazov is $nazov[strlen($nazov)-1], so use < instead of <= in the loop condition:
for($i=0;$i<strlen($nazov);$i++)

Related

Uninitialized string offset php

I am writting my own calculator on PHP.
I have a problem with my code because i don't know where i am trying to read too far in the string. So if anyone can enlighten me ..
The exact error i get is :
PHP Notice: Uninitialized string offset: 4 in /home/salim/Bureau/web/piscine_php/d01/ex11/do_op_2.php on line 76
Here is the code below :
function decoupe ($argv)
{
global $nbr1;
global $nbr2;
global $sign;
$string = NULL;
$string = trim($argv[1], " \t");
echo $string;
echo "\n";
$x = 0;
while($string[$x])
{
if (is_numeric($string[0]) == false)
error_msg();
if (is_numeric($string[$x]) && $string[$x + 1])
{
while (is_numeric($string[$x]))
{
$nbr1 .= $string[$x];
$x++;
}
}
if (is_thisoperator(substr($string, $x)))
{
$sign .= $string[$x];
$x++;
}
else
{
error_msg();
}
if ($string[$x + 1] && is_numeric($string[$x]))
{
while (is_numeric($string[$x]))
{
$nbr2 .= $string[$x];
$x++;
}
}
else
{
error_msg();
}
}
Don't use $string[$x] as a way to test whether $x is a valid index in the string. It prints a warning when $x is outside the string. Use $x < strlen($string) instead. So change:
while ($string[$x])
to
while ($x < strlen($string))
and change
if ($string[$x + 1] && is_numeric($string[$x]))
to
if ($x + 1 < strlen($string) && is_numeric($string[$x]))

Uninitialized string offset: -1 when referencing single string character with curly braces

I have this code:
$len = strlen($string);
if ($string{$len-1} == '-') {
// Do stuff...
}
However I get the following NOTICE error:
Uninitialized string offset: -1
When I var_dump($len-1) the value I get:
int 3
When I var_dump($string) I get:
string 'bobo' (length=4)
So could anyone tell me why this is causing a NOTICE error?
To prevent notice add additional condition to if statement:
$len = strlen($string);
if ($len > 0 && $string{$len-1} == '-')
// Do stuff...
}
or use substr function:
if (substr($string, -1) == '-') {
// Do stuff...
}
$len = strlen($string);
if(isset($string{$len-1})){
if ($string{$len-1} == '-') {
// Do stuff...
}
}

PHP Excel Reader 2 read can't read column if not filled

I've got an excel file like this: Excel example
I want to read the columns F, G ,H ,J when I do:
for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
error_log(
FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][6]).' - '.
FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][7]).' - '.
FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][8]).' - '.
FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][9]));
$count++;
}
I have the problem that I get errors:
PHP Notice: Undefined offset: 7 PHP Notice: Undefined offset: 8
PHP Notice: Undefined offset: 9
the F column is always full, but as you can see I've got problems with the others. How can I escape this error, so the field only gets checked if it's filled with data?
for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
$err=array;
for($j=6; $j<=9; $j++){
if(isset(FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][$j])))
$err[]=FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][$j]) ;
}
if(!empty($err){
$msg=implode('-',$err);
error_log($msg);
}
$count++; //I don't know what you're using $count for
}
if (isset($data->sheets[0]['cells'][$i][7])) {
$col7 = $data->sheets[0]['cells'][$i][7];
} else {
$col7 = '';
}
if (isset($data->sheets[0]['cells'][$i][8])) {
$col8 = $data->sheets[0]['cells'][$i][8];
} else {
$col8 = '';
}
if (isset($data->sheets[0]['cells'][$i][9])) {
$col9 = $data->sheets[0]['cells'][$i][9];
} else {
$col9 = '';
}
looks realy ugly but it works.

Notice: Uninitialized string offset: 62 in /.../libraries/functions.php on line 316

Man oh man I cannot figure this out... Please help.
What a missing here?
here are the errors:
Notice: Uninitialized string offset: 62 in /..../libraries/functions.php on line 316
Notice: Undefined variable: stilldo in /..../libraries/functions.php on line 322
Here is the code:
function generate_password($length = 12, $letters = true, $mixed_case = true, $numbers = true, $punctuation = false){
## Generate the alfa
$alfa = '';
if($letters == true) $alfa .= 'abcdefghijklmnopqrstuvwxyz';
if($mixed_case == true) $alfa .= strtoupper('abcdefghijklmnopqrstuvwxyz');
if($numbers == true) $alfa .= '0123456789';
if($punctuation == true)$alfa .= '~!##$%^&*(),.=+<>';
## Set Random Seed
if(!function_exists('_generate_password_seed')):
function _generate_password_seed(){
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
endif;
srand(_generate_password_seed());
## Generate the password
$token = "";
for($i = 0; $i < $length; $i ++) {
$token .= $alfa[#rand(0, #strlen($alfa))];
}
## Check the length
if(strlen($token) < $length){
// echo $stilldo = $length - strlen($token);
$token .= generate_password($stilldo);
}
## Return the password
return $token;
}
I get this error 5 out of 10 times i run this function and I can't seem to crack it.
Notice: Undefined variable: stilldo means, that a variable $stilldo is not defined, nor initialized... That means, that You call function generate_password in recursion when passing it an undefined variable, that in fact has a null value. Therefore Your function cannot work properly.
The part of your code should look like this:
## Check the length
if(strlen($token) < $length){
$stilldo = $length - strlen($token);
$token .= generate_password($stilldo);
}
Yes, a stupid mistake - You commented out the right line (where only echo should be commented out or deleted).
Enjoy!

preg_match in preg_match_all

$match_expression = '/<a href="look.php\?id=(.*)" title="Look page: (.*)">(.*)<\/A>/';
$radompgr = preg_match_all($match_expression,$q2,$match, PREG_SET_ORDER);
if($radompgr == TRUE){echo "found $radompgr<br>";}else{echo "not found $radompgr<br>";} //found
for ($i = 0; $i < count($match); $i++) {
$mathcas = $match[$i][1];
$radom = preg_match('/[0-9a-z]{39,41}/',$mathcas,$matches2);
if($radom == TRUE){
$match11 = $matches2[1];
echo "found".$i.": ".$match11."";}else{echo"".$i."not found :(<br>";}
} // "found0", but don`t show $match11 variable.
Show "found0", but don`t show $match11. How to do $match 11 to show?
Return:
Notice: Undefined offset: 1 in C:\xampp\htdocs\page.php on line 75
found0:
Notice: Undefined offset: 1 in C:\xampp\htdocs\copy\page.php on line 75
found1:
Notice: Undefined offset: 1 in C:\xampp\htdocs\copy\page.php on line 75
found2:
Sorry if my English is not perfect, I'm not a native. :)
Thank you for your help.
You forgot to enclose the () in preg_match():
$match_expression = '/<a href="look.php\?id=(.*)" title="Look page: (.*)">(.*)<\/A>/';
$radompgr = preg_match_all($match_expression, $q2, $match, PREG_SET_ORDER);
if ($radompgr >= 1)
{
echo 'found ' . $radompgr;
for ($i = 0; $i < count($match); $i++)
{
$mathcas = $match[$i][1];
$radom = preg_match('/([0-9a-z]{39,41})/', $mathcas, $matches2);
if ($radom >= 1)
{
$na = $matches2[1];
echo 'found' . $i . ': ' . $na;
}
else
{
echo $i . 'not found';
}
}
}
else
{
echo 'not found ' . $radompgr;
}

Categories