Solved

Reading line problem

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 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