473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

recognizing and reading a new line character

In my program I have an if else statement where I'm trying to see if that line is a skipped line. My text is like this:

fruits

vegetables.

So it has to be able to know to skip to the next line if theres no text in that line.

Expand|Select|Wrap|Line Numbers
  1. if ($invalidcheck[0]=='\r\n'){
  2.         $i++;
  3.         }
  4.  
but it doesn't seem to be reading it. Does anyone know how I can fix this? Also I was wondering if it is reasonable to try to do something like if the character isn't a-z, A-Z, and all symbols.
Thanks
Jun 11 '09 #1
6 2604
Atli
5,058 Expert 4TB
Try using the trim and empty functions to check the value.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $value = "\r\n  ";
  3.  
  4. $value = trim($value);
  5. if(empty($value)){
  6.     echo "Empty";
  7. }
  8. else {
  9.     echo "Not empty";
  10. }
  11. ?>
@phpnewbie26
Whether it is reasonable to do, that's up to you, and whoever else is designing your application.

If it is, you can easily do that using regular expressions.
Like:
Expand|Select|Wrap|Line Numbers
  1. if(preg_match('/^[a-zA-Z]+$/i', $value))
This would only allow the $value to contain letters.
(And only letters... no numbers, spaces, question marks, etc...)
Jun 11 '09 #2
The trim and empty function worked perfectly. Thanks! I'm new to php so I wasn't even aware of that function.=)
Jun 11 '09 #3
Markus
6,050 Expert 4TB
@Atli
Shouldn't that be [^a-zA-Z0-9]? Or do they both work the same?
Jun 11 '09 #4
Markus
6,050 Expert 4TB
@phpnewbie26
You should try and use other options, instead of the preg_* functions as they're said to be pretty slow (the preg_* functions), although it's probably such a small difference it's pointless bringing it up (will have to benchmark it).
Jun 11 '09 #5
Atli
5,058 Expert 4TB
@Markus
Just different varieties.

Mine was like:
Expand|Select|Wrap|Line Numbers
  1. if(<only allowed chars found>) {
  2.   // Do stuff
  3. }
  4. else {
  5.   // Don't do stuff
  6. }
But yours is more like:
Expand|Select|Wrap|Line Numbers
  1. if(<illegal chars found>) {
  2.   // Don't do stuff
  3. }
  4. else {
  5.   // Do stuff
  6. }
Whitelist vs. Blacklist approaches... although in this case, with the ^ in the regex class, they are not so different.
Jun 11 '09 #6
Atli
5,058 Expert 4TB
@phpnewbie26
Glad it worked :)
Jun 11 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
9
by: Jukka K. Korpela | last post by:
I noticed that Internet Explorer (6.0, on Win XP SP 2, all fixes installed) incorrectly renders e.g. &harr &euro &Omega literally and not as characters denoted by the entities, but if a semicolon...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
6
by: Gus Tabares | last post by:
Hello, I'm having trouble reading in a character. Here is a snippet of code: int num; char character; printf("Enter a number: "); scanf("%d", &num); printf("You entered %d.\n", num);
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
6
by: The_Kingpin | last post by:
Hi again guys, I've decided to cut my project in section and I found it way easier like this. I'm having a little problem reading struct in a file though. I think after this I'll be able to...
2
by: SteMc | last post by:
today I tackled, for the first time, opening and reading from a text file. Following the example on the MSDN and declared a variable, strline as a string and objstreamreader as a streamreader. ...
3
by: NaN | last post by:
Hi I'm using Dev-C++. Here is my sourcecode. /* GETCH.C: This program reads characters from * the keyboard until it receives a 'Y' or 'y'. */ #include <conio.h>
9
by: Amkcoder | last post by:
I am trying to read a file a character at a time, I want to read in the new line character. I am printing out each character to the screen. For some reason it will not print out the new line. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
marktang
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.