Solved

Reading line problem

Posted on 2002-06-14
2
190 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

760 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

17 Experts available now in Live!

Get 1:1 Help Now