Solved

PHP string from an array element

Posted on 2007-03-21
3
224 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now