Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-10-05
5
Medium Priority
?
67 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 44

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 60

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 44

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
This article discusses four methods for overlaying images in a container on a web page
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…
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.
Suggested Courses

916 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