Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP string from an array element

Posted on 2007-03-21
3
Medium Priority
?
234 Views
Last Modified: 2010-07-27
for ($i = 0; $i < count($lines); $i++)
{
if (strpos($lines[$i],"The river comes") >= 0)
{
      $current = $lines[$i];
}
}

This condition does show as true exactly once which is right.  $current, however,  is always just a return character.  I am getting so aggravated with this.
I have tried this using strval with the same result.
0
Comment
Question by:okerupt
[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
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 1000 total points
ID: 18768789
When "The river comes" is at the start of the string, strpos returns 0 and $current is set. However, when it's not found at all, strpos returns false which seems to also evaluate to 0, so $current is also set (but it shouldn't be).

Try changing your test to:
  if (is_numeric(strpos($lines[$i],"The river comes")))
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 18768793
Just so you know, I tested it as follows:

$lines = array("When The river comes by",
               "blah",
               "The river comes through");
for ($i = 0; $i < count($lines); $i++) {
  print "Searching $i: ".$lines[$i]."<br>\n";
  if (is_numeric(strpos($lines[$i],"The river comes"))) {
    $current = $lines[$i];
    print "Found: $current at pos ".strpos($lines[$i],"The river comes")."<br>\n";
  }
}

displays:

Searching 0: When The river comes by
Found: When The river comes by at pos 5
Searching 1: blah
Searching 2: The river comes through
Found: The river comes through at pos 0
0
 

Author Comment

by:okerupt
ID: 18770623
You're right about false = 0.  As a .net programmer that irritates me.
0

Featured Post

Independent Software Vendors: 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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

609 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