I have this piece of code which works well if the ['extensions'] array exists.. but if the array does not exist then it returns errors. How can I fix this code to not return anything if the extensions array does not exist?
-[UPDATE-
Sorry i had previously inserted the wrong code.. here is the proper code i need checked.
$oid = array('id-ce-subjectAltName');
$count = count($cert['tbsCertificate']);
for($i = 0; $i < $count; $i++) {
if(array_key_exists('extensions', $cert['tbsCertificate']) &&
in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n",'<b>[SANs]</b>',"\n","\n";
}
}
I get this warning when ['extensions'] does not exist - I would like to prevent any warnings from being generated.
Notice: Undefined index: extensions in
C:\xampp\htdocs\labs\certdecode\certdecode.php on line 142
AFTER UPDATE:
How is the structure of the array?
You count the number of items in $cert['tbsCertificate'],
but in your loop, your $i is for the number of items in $cert['tbsCertificate']['extensions'].
So maybe you try to do something like this?:
$oid = array('id-ce-subjectAltName');
if (array_key_exists('extensions', $cert['tbsCertificate']) &&
is_array($cert['tbsCertificate']['extensions'])
) {
$count = count($cert['tbsCertificate']['extensions']);
for ($i = 0; $i < $count; $i++) {
if (in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n", '<b>[SANs]</b>', "\n", "\n";
}
}
}
okay, seems to work by adding an isset() at the start of the code:
if(isset($cert['tbsCertificate']['extensions'])) {
thanks guys!
Related
How can I have same data stored as much as provided number?
Example
Store abc, 10 times
both abc and 10 are coming from form request
Code
nonuiqueAmount: 10
nonuiqueSerial: "abc"
if(!empty($request->input('nonuiqueSerial'))) {
foreach($request->input('nonuiqueAmount') as $item) { // this returns error
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
}
Error:
Invalid argument supplied for foreach()
You should use a for loop:
// nonuiqueAmount: 10
// nonuiqueSerial: "abc"
if (!empty($request->input('nonuiqueSerial'))) {
for ($i = 0; $i < $request->input('nonuiqueAmount', 0); ++$i) { // I've added the zero as a default value to prevent unnecessary loops
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
}
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. w3schools docs
Your nonuiqueAmount is an int. I would suggest simply stick with basic for loop
for ($x = 0; $x < $request->input('nonuiqueAmount'); $x++) {
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
I'm having some doubt doing some for each loop, so i have an immense variable names ranging from $a1 - $a120
What I'm trying to do is doing a for each loop from where I can get each of thoose by using an indexing system.
$a116= "N69";
$a117= "V52";
$a118= "V53";
$a119= "V54";
$a120= "V55";
# FIM
for ($i = 0; $i <= 119; ++$i) {
$var = ${"a".$i}; // This is what i need to learn to do
$sheet->setCellValue($var, $array[$i]); // the array is other information im inserting to the file
}
It is not good for the loops. But you can use it, if you can not change your codes.
I just added
$var_name="a".$i;
$var = $$var_name;
And the full code is below.
$a116= "N69";
$a117= "V52";
$a118= "V53";
$a119= "V54";
$a120= "V55";
# FIM
for ($i = 0; $i <= 119; ++$i) {
$var_name="a".$i;
$var = $$var_name; // This is what i need to learn to do
$sheet->setCellValue($var, $array[$i]); // the array is other information im inserting to the file
}
You should update the code to use an array.
$data = [];
$data["a116"] = "N69";
$data["a117"] = "V52";
$data["a118"] = "V53";
$data["a119"] = "V54";
$data["a120"] = "V55";
Now you can use a foreach loop getting the key/value pairs
foreach($data as $key => $value){
$sheet->setCellValue($key, $value);
}
I am looking for a PHP solution to use a loop to go through to capture all the data
Here is an example of a lookup without using a loop
if (array_key_exists('utf8String', $cert['tbsCertificate']['subject']['rdnSequence'][0][0]['value'])) {
// do somthing
} else if (array_key_exists('printableString', $cert['tbsCertificate']['subject']['rdnSequence'][0][0]['value'])) {
// do somthing
} else if (array_key_exists('bmpString', $cert['tbsCertificate']['subject']['rdnSequence'][0][0]['value'])) {
// do somthing
} else if (array_key_exists('telextexString', $cert['tbsCertificate']['subject']['rdnSequence'][0][0]['value'])) {
// do somthing
}
I need the loop to go through the entire array. For ONLY the first [ ] the loop should increase the integer [0] to 1, [2] and so forth until its gone through the whole lot. In case you are wondering, the second [ ] is always [0] so that needs to remain as is.
Right now I am copying/pasting the above about 20 times and manually updating the number in the first box but I am hoping there is a more elegant way to achieve that.
-- MORE CONTEXT --
-- WORKING CODE -- offered by #Ghost
$count = count($cert['tbsCertificate']['subject']['rdnSequence']);
$exists = array('utf8String', 'printableString', 'teletexString');
$oid = array('id-at-stateOrProvinceName', 'id-at-countryName', 'id-at-localityName', 'id-at-commonName', 'id-at-organizationalUnitName');
for($i = 0; $i < $count; $i++) {
foreach($exists as $field) {
if(array_key_exists($field, $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value'])) {
$value = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value'][$field];
echo $value, ' [',$field, ']',"\n";
}
}
}
You can just add another loop inside applying each field into array_key_exists, this applies to #Markus' idea anyway:
$count = count($cert['tbsCertificate']['subject']['rdnSequence']);
$exists = array('utf8String', 'printableString', 'teletexString');
$oid = array('id-at-stateOrProvinceName', 'id-at-countryName', 'id-at-localityName', 'id-at-commonName', 'id-at-organizationalUnitName');
for($i = 0; $i < $count; $i++) {
foreach($exists as $field) {
if(array_key_exists($field, $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value'])) {
$value = $cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['value'][$field];
$k = array_keys($cert['tbsCertificate']['subject']['rdnSequence'][$i][0]['type']);
$oid = reset($k);
break;
}
}
}
[ EDIT: Please see the comments below. ] How about for simples...
$strings = ['utf8String', 'printableString' ... ];
foreach ($strings as $string) { // do your checks etc. }
I suppose you know how to increment a counter in a loop. $i++ and stuff, use [$i] wherever you need to increment the reference value in your $cert array. On match, break or continue in place of else if, depending on what exactly you need to accomplish here. Your objectives aren't too clear in the question, could share a bit more insight...
<?
for ($i=0; $i<=9; $i++) {
$b=urlencode($cl[1][$i]);
$ara = array("http://anonymouse.org/cgi-bin/anon-www.cgi/", "http%3A%2F%2Fanonymouse.org%2Fcgi-bin%2Fanon-www.cgi%2F");
$degis = array("", "");
$t = str_replace($ara, $degis, $b);
$c="$t";
$base64=base64_encode($t);
$y=urldecode($t);
$u=base64_encode($y);
$qwe = "http://anonymouse.org/cgi-bin/anon-www.cgi/$y";
$ewq = "h.php?y=$u";
$bul = ($qwe);
$degistir = ($ewq);
$a =str_replace($bul, $degistir, $ic);
}
?>
when i put $cl[1][0], $cl[1][1], $cl[1][2] works successfull but when i put $i its returning null. why is this happening?
**I'm trying to change EACH url to base64 codes that I received from remote url with preg_match_all **
Have you checked that $c1[1] has 10 elements? (From $c1[1][0] to $c1[1][9] there are 10 elements, not 9.
Maybe you are getting null for the last one $c1[1][9]. Try to do a var_dump($c1[1]) to check that it contains all the elements that you expect.
Update:
Change the this line
for ($i=0; $i<=9; $i++) {
into this
for ($i=0; $i<9; $i++) {
So I have fields that are generated dynamically in a different page and then their results should posted to story.php page. fields is going to be : *noun1 *noun2 *noun3 and story is going to be : somebody is doing *noun1 etc. What I want to do is to replace *noun1 in the story with the *noun, I have posted from the previous page ( I have *noun1 posted from the previous page ) but the code below is not working :
$fields = $_POST['fields'];
$story = $_POST['story'];
$fieldsArray = split(' ', $fields);
for ($i = 0; $i < count($fieldsArray); $i++) {
${$fieldsArray[$i]} = $_POST[$fieldsArray[$i]];
}
// replace words in story with input
for ($i = 0; $i < count($story); $i++) {
$thisWord = $story[$i];
if ($thisWord[0] == '*')
$story[$i] = ${$thisWord.substring(1)};
}
$tokensArray = split(' ',$tokens);
echo $story;
Your problem is likely that you are trying to echo $story, which I gather is an array. You might have better luck with the following:
$storyString = '';
for ($i = 0; $i < count($story); $i++)
{
$storyString .= $story[i] . ' ';
}
echo $storyString;
echo can't print an array, but you can echo strings to your heart's content.
You almost certainly don't want variable variables (e.g. ${$fieldsArray[$i]}). Also, $thisWord.substring(1) looks like you're trying to invoke a method, but that's not what it does; . is for string concatenation. In PHP, strings aren't objects. Use the substr function to get a substring.
preg_replace_callback can replace all your code, but its use of higher order functions might be too much to get into right now. For example,
function sequence($arr) {
return function() {
static $i=0
$val = $arr[$i++];
$i %= count($arr);
return $val;
}
}
echo preg_replace_callback('/\*\w+/', sequence(array('Dog', 'man')), "*Man bites *dog.");
will produce "Dog bites man." Code sample requires PHP 5.3 for anonymous functions.