Solved

Using strpos to find first occurence but not working for some reason

Posted on 2016-10-05
5
47 Views
Last Modified: 2016-10-05
HI,
can you see what I'm doing wrong? I am expecting given the code below to have category_id echo out '16' based on strpos finding 'Sport Utility'.

<?php


$trim_style = "Sport Utility";

$category_aliases = array(
  "Sport Utility" => 16,
  "4dr Car" => 17,
  "HB" => 21,
  "Hbk" => 21,
  "2dr Car" => 19,
  "Convertible" => 20,
  "Truck\nPickup\nRegular Cab Pickup - Short Bed\nRegular Cab Pickup - Long Bed\nCrew Cab Pickup - Short Bed\nCrew Cab Pickup - Long Bed" => 15,
  "Station Wagon" => 13
);


$category_id = false;

		foreach ($category_aliases as $alias => $id) {
			if(strpos($trim_style, $alias))
			{
				$category_id = $id;
			}
		}
		
	echo $category_id;


?>

Open in new window

0
Comment
Question by:tjyoung
5 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41830079
PHP strpos() may return zero, which is loosely interpreted as FALSE in the context of the if() statement.  Check the man page.  You may want to change the test to use an identicality for !False (not false).
https://iconoun.com/demo/temp_tjyoung.php
<?php // demo/temp_tjyoung.php
/**
 * https://www.experts-exchange.com/questions/28974502/Using-strpos-to-find-first-occurence-but-not-working-for-some-reason.html
 *
 * http://php.net/manual/en/function.strpos.php
 */
error_reporting(E_ALL);

$trim_style = "Sport Utility";

$category_aliases = array(
  "Sport Utility" => 16,
  "4dr Car" => 17,
  "HB" => 21,
  "Hbk" => 21,
  "2dr Car" => 19,
  "Convertible" => 20,
  "Truck\nPickup\nRegular Cab Pickup - Short Bed\nRegular Cab Pickup - Long Bed\nCrew Cab Pickup - Short Bed\nCrew Cab Pickup - Long Bed" => 15,
  "Station Wagon" => 13
);


$category_id = false;

foreach ($category_aliases as $alias => $id) {
    if(FALSE !== strpos($trim_style, $alias)) {
        $category_id = $id;
    }
}

echo $category_id;

Open in new window

Outputs:
16

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41830096
Yep that was totally it.
Thanks again for the direction.
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41830098
You need to qualify your condition, such as:

if(strpos($trim_style, $alias) > -1)

Open in new window


or

if(strpos($trim_style, $alias) !== FALSE)

Open in new window

0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41830099
Ray has answered this but to elaborate
$trim_style = "Sport Utility";

$category_aliases = array(
  "Sport Utility" => 16,

Open in new window

If you compare the two strings Sport Utility is found at position 0 of the first element of the array (arrays and strings being 0 indexed)

So strpos is going to return a 0 for the comparison which means I found one at position 0 - NOT I did not find one.

Change to (as Ray suggests)
if(strpos($trim_style, $alias) !== false)

Open in new window

And it should work.
!== means the returned value must be the value false - not a falsy value (Null, empty, 0, false)
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41830101
oops.... a second late to the party ;)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

791 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