?
Solved

PHP string from an array element

Posted on 2007-03-21
3
Medium Priority
?
232 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 look for a specific file type in a local or remote server directory using PHP.
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 …
Suggested Courses

801 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