I am trying to read the first character in an array but my syntax seems to be wrong as it says I have an error. I have read each line a text file into an array and by using fgetc, I'm trying to see if the first character of each line is an "[". My current coding is: - if(fgetc($invalidcheck[$i])=='[')
Any help would be appreciated. Thanks!
11 3126 Markus 6,050
Recognized Expert Expert @phpnewbie26
fgetc() expects a file resource handle as it's parameter.
Incidentally, you can check the first character of a string by accessing it like you would an array: -
$string = "[hi]";
-
-
if ($string[0] == "[") {
-
echo "yessir";
-
}
-
hm..doesnt seem to be working. My problem is that I don't know how to point to let's say $string[2] and get it's first character. I tried $string[2][0] but it still didnt work.=/
okay.=) - $invalidcheck=fgets($invalid);
-
for($i=0;$i<count($invalidcheck);$i++){
-
if (i==0){
-
$tmp=explode('=',$invalidcheck[$i]);
-
if ($tmp[0]!=='MYNAME'){
-
die ("typo in name.");
-
}
-
else{
-
continue;
-
}
-
elseif ($invalidcheck[$i][0]=='['){
-
$tmp2=substr($invalidcheck[$i],1,6);
-
if ($tmp2==$todaydate){
-
$counter=1;
-
}
-
else {$counter=0;}
-
}
-
else{
-
$parts2=explode(' % ',$invalid[$i]);
-
$parts3=explode('=', $parts2[0]);
-
$parts4=explode('.', $parts3[0]);
-
$check[0]=$parts4[0];
-
$check[1]=$parts4[1];
-
if(array_search("$check[0]", $items) && array_search("$check[1]", $items)){
-
continue;}
-
else{
-
or die("Found error in project name/code.");
-
}
-
}
-
fclose($invalid);
-
It's the 11th line. Thanks
dlite922 1,584
Recognized Expert Top Contributor
Troubleshooting is an aquired skill,
right after your for loop do a print_r() or var_dump() on your variable to track what they contain and see if they contain the value you expect them to.
When you say it's not working, we expect there's a PHP error, if there isn't, it's most likely has to do with your input/data.
Cheers,
Dan
hm..it still doesnt seem to be working. Let's say I have an array like so:
array([0]=>'red', [1]=>'green', [2]=>'blue')
how do i read let's say..the g in green.
Markus 6,050
Recognized Expert Expert @phpnewbie26 -
$array = array('red', 'green', 'blue'); // Note: this is the same as the example you gave.
-
-
$element = $array[1]; // green
-
echo $element[0]; // g
-
Dormilich 8,658
Recognized Expert Moderator Expert Markus 6,050
Recognized Expert Expert
thanks guys! all of your ideas worked=).
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: William L. Bahn |
last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm
looking for an explanation for the following:
The functions pairs:
gets()/fgets()
puts()/fputs()
printf()/fprintf()...
|
by: Orion |
last post by:
Hey,
I was wondering if it was possible to determine if you hit 'EOF' using
fseek? I'm using fseek to traverse through the file from start to end
and capturing the data into a linked list...
|
by: Kobu |
last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to
take input or an operating system call function like read() (I know it
could be any "way", but I'm trying to find out how it's...
|
by: Mark |
last post by:
i'm trying to read a file one char at a time into a char array thusly...
char buffer;
while (readChars(InputFile, buffer, BUFFER_SIZE) != 0) {
//not doing anything atm
}
int readChars(FILE...
|
by: cinsky |
last post by:
Hi,
While reading ISO C Standard, I found follow text in 7.19.8.1:
size_t fread(void *restrict ptr, size_t SIZE, ...)
... For each object, SIZE calls are made to the fgetc function
and the...
| |
by: 01 |
last post by:
#include <stdio.h>
int main ( int argc, char *argv )
{
if ( argc != 2 ) /* argc should be 2 for correct execution */
{
/* We print argv assuming it is the program name */
printf( "usage: %s...
|
by: Ivan |
last post by:
Hi,
What is the best syntax to use a char to index into an array.
///////////////////////////////////
For example
int data;
data = 1;
|
by: kindrain |
last post by:
the code checks whether a.txt has exact the same lines, then write
different lines into b.txt
Here it is:
(before that ,you should creat a.txt)
----------------------
#include<stdio.h>...
|
by: bmerlover |
last post by:
How can you read a file in a timer function? I am currently using a do while statement to read a file which works fine. The implementation is in C but I've added the code to a C++ .NET framework GUI...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |