preg_match in preg_match_all - php

$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;
}

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]))

How to fix undefined offset and undefined index

Hello I have this code which selects from a list of 20 items 12 and from those 12 1 is randomly selected and echoed:
class Gamble
{
public static function doPointsCheck()
{
global $Gamble;
$Gamble_points = '2';
if($Gamble_points <= 1)
return false;
else
return true;
}
public static function Spin()
{
global $Wheel;
if(!self::doPointsCheck())
return 'You need way too more points to gamble';
else
{
$Result = array();
$Result = range(1, 12);
shuffle($Result);
$Result = array_slice($Result, 0, 20);
$SendToCheck = self::CheckPrize($Result);
}
}
public static function CheckPrize($Array)
{
global $Wheel;
echo '<h1>List of items you can win today:</h1><form action="class.MysticWheel.php" method="post">';
for($i = 1; $i <= count($Array); $i++)
{
$Won = '<p>'.$Wheel[$Array[$i]]['pseudo'].'</p>';
echo $Won;
}
echo '<input name="Feeling lucky" type="submit" value="Feeling Lucky" /></form>';
if ($_SERVER['REQUEST_METHOD'] === 'POST'){
$i = rand( 1, 12 );
$Prize = '<p>'.$Wheel[$Array[$i]]['pseudo'].'</p>';
echo $Prize;
}
}
}
When I test it I recieve the following error:
Notice: Undefined offset: 12 on line 54
Notice: Undefined index: on line 54
So i won't show the last item, it will only show 11 possible items that could have been won and get the notice.
How can I fix it?
$Result = range(1, 12);
-->
foreach (range(1, 12) as $number) {
$Result[] = $number;
}
Try this.

Uninitialized string offset error in simple for loop 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++)

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.

How to get rid of PHP Undefined offset error?

How to get rid of error: Undefined offset: 1 on line 20? I know it occurs becaouse Im calling array that's offset simply does not exist.
Silecing it wiht # just doesnt seem right at all.
//create an array with all x, y
for ($x = 1; $x <= 5; $x++) $array_x[] = $x;
for ($y = 1; $y <= 5; $y++) $array_y[] = $y;
$IN_x = "'" . implode("', '", $array_x) . "'";
$IN_y = "'" . implode("', '", $array_y) . "'";
$pullMapInfo = "SELECT x, y, value FROM mapinfo WHERE id='{$id}' AND x IN ({$IN_x}) AND y IN ({$IN_y})";
$pullMapInfo2 = mysql_query($pullMapInfo) or die('error here');
//create an associative array x, y => value
while ($pullMapInfo3 = mysql_fetch_assoc($pullMapInfo2)) {
$result[ $pullMapInfo3['x'] ][ $pullMapInfo3['y'] ] = $pullMapInfo3['value'];
}
//loop to display output
foreach ($array_x as $x) {
foreach ($array_y as $y) {
if (array_key_exists($x, $result) && array_key_exists($y, $result)) {
echo '<div class="castle_array" id="'.$x,'x',$y.'">'. $result[$x][$y] .'</div>
';
} else {
echo '<div class="castle_array" id="'.$x,'x',$y.'"></div>
';
}
}
}
2 for loops seems awkard
Replace this line:
if (array_key_exists($x, $result) && array_key_exists($y, $result)) {
with:
if (isset($result[$x][$y])) {
Documentation: isset
For Joseph Silber: Try this PHP fiddle
Your second call to array_key_exists should pass in $result[$x]:
if ( array_key_exists($x, $result) && array_key_exists($y, $result[$x]) ) {
// Code goes here...
}

Categories