?
Solved

Reading line problem

Posted on 2002-06-14
2
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
axis_img earned 200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
Suggested Courses

770 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