Solved

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

Posted on 2016-10-05
5
58 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 110

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 58

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
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.

627 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