Solved

PHP string from an array element

Posted on 2007-03-21
3
228 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
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 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