Solved

Reading line problem

Posted on 2002-06-14
2
203 Views
Last Modified: 2006-11-17
Lo all,

Im trying to use an if-statement on a line from a textfile. The textfile looks something like this:

------------------------------------------------
[new]
bla bla bla
------------------------------------------------

Im using the following code to analyse textfiles like these, but something seems to go wrong :(

------------------------------------------------
<?php
$fileLines = file("data/news.txt");
foreach($fileLines as $value) {
  if ($value=="[New]"){
    echo "New item found<BR>";
  }
  else {
    echo $value."<BR>";
  }
}  
?>
------------------------------------------------

Can somebody tell me how i can get the if-statement to echo the "New item.." when that line passes the loop? Cuz i dont get it :)

Tnx
Rob van Lokven
0
Comment
Question by:rvlokven
2 Comments
 
LVL 7

Accepted Solution

by:
axis_img earned 50 total points
ID: 7079715
Do you want this if() check to be case-sensitive?

if($value == "[New]")

will not match the line you pasted above, which was:

[new]
bla bla bla

[New] != [new]

If you don't care about the case, you can do a couple of things...

First thing, you can lowercase the $value so you know for sure that the case will match your term, and then make sure the term you are matching against is in lowercase:
if(strtolower($value) == "[new]")

Also, I would suggest using trim() on the value from the text file, just in case there are any rogue whitespace characters attached to the line. It will remove any extra spaces from the ends of the line (left and right side, not in between).

if(trim(strtolower($value)) == "[new]")

For documentation:
http://www.php.net/manual/en/function.strtolower.php
http://www.php.net/manual/en/function.trim.php

That should give you what you are looking for. If you want to make sure it is case-sensitive, then what you are doing now will work perfectly. The only thing I would add is the trim() statement. It may or may not be needed, but it is a painful little bug to find when it does become a problem.

Let me know if you have any other questions...
Barry
0
 

Author Comment

by:rvlokven
ID: 7080174
Good solution and a lot of extra info, tnx alot :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now