Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-10-05
5
Medium Priority
?
64 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 59

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

660 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