?
Solved

Need to find a chareter in a string

Posted on 2014-07-16
14
Medium Priority
?
179 Views
Last Modified: 2014-07-20
I need to find if a character is in a string
What's the easiest way

$var1 = "d"
$var2 = LKJOUdsiZXD"

I just need a true or false.  I don't care where it is.
0
Comment
Question by:breeze351
11 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40200389
strpos() returns FALSE if the character is not found.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40200408
if(strpos($var2, $var1)>0 ){
// Then do something
}
else {
echo "Not found";
}

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 40200418
Example here: http://iconoun.com/demo/temp_breeze351.php
<?php // demo/temp_breeze351.php
error_reporting(E_ALL);
echo '<pre>';

// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28477487.html
// REF: http://www.php.net/manual/en/function.strpos.php

// TEST DATA
$str = 'LKJOUdsiZXD';

// SEARCH STRING
$chr = 'd';
if (strpos($str, $chr) !== FALSE) echo PHP_EOL . "CHR $chr FOUND IN $str";
if (strpos($str, $chr) === FALSE) echo PHP_EOL . "CHR $chr NOT FOUND IN $str";

$chr = '?';
if (strpos($str, $chr) === FALSE) echo PHP_EOL . "CHR $chr NOT FOUND IN $str";

Open in new window

HTH, ~Ray
0
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.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40200431
Gary: Please see the man page warning about equality comparisons.
http://www.php.net/manual/en/function.strpos.php

The use of > 0 is one of the common trip-wires.  PHP numbers string positions from zero, not from 1.  As a result, a substring or character found in the leftmost position of the string will cause strpos() to return zero.  That is why we want to test for FALSE with identicality using === or !== to check the return from this function.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40200515
Correct, I always forget that with strpos
Can fix by changing the first line to

if(strpos("0".$var2, $var1)>0 ){
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 1000 total points
ID: 40200524
If you don't care about case use stripos
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40200699
if(strpos("0".$var2, $var1)>0 ){
unless $var1 happens to be "0" and $var2 has a leading zero.  That's why I trust the test for FALSE.  It's what the function returns when the string is not found.
0
 

Author Comment

by:breeze351
ID: 40202651
Thanks
This logic is going to get harder and I'm getting a headache.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40206320
Reinstate the original accepted answer and if anyone has a problem they can use the Request Attention link
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
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 count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

864 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