Solved

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

Posted on 2016-10-05
5
35 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 108

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 42

Expert Comment

by:zephyr_hex
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 51

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 42

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Encode a password 2 38
Responsive Design Tools 6 36
echo button 13 45
php documentation 4 19
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now