Need to find a chareter in a string

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.
breeze351Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
strpos() returns FALSE if the character is not found.
0
GaryCommented:
if(strpos($var2, $var1)>0 ){
// Then do something
}
else {
echo "Not found";
}

Open in new window

0
Ray PaseurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Ray PaseurCommented:
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
GaryCommented:
Correct, I always forget that with strpos
Can fix by changing the first line to

if(strpos("0".$var2, $var1)>0 ){
0
Marco GasiFreelancerCommented:
If you don't care about case use stripos
0
Ray PaseurCommented:
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
breeze351Author Commented:
Thanks
This logic is going to get harder and I'm getting a headache.
0
GaryCommented:
Reinstate the original accepted answer and if anyone has a problem they can use the Request Attention link
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.