Solved

Reading line problem

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Query 5 43
PHP website on Linux - server DNS address could not be found. 18 82
issue with DB import 1 36
scan php uploads for viruses 5 24
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
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…
The viewer will learn how to dynamically set the form action using jQuery.

763 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