Find - in a word

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Find - in a word

Post by xionhack »

Hello. I want to make a function that will store a string in a variable. If there is a dash (-) in the string, it will set the variable to NULL, if it has no dashes then it will store that string in the variable. How can i do that? Thanks!
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Find - in a word

Post by koen.h »

xionhack wrote:Hello. I want to make a function that will store a string in a variable. If there is a dash (-) in the string, it will set the variable to NULL, if it has no dashes then it will store that string in the variable. How can i do that? Thanks!
you can use something like this:

Code: Select all

$variable = strrpos($string, "-") ? $string : null;
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Find - in a word

Post by requinix »

koen.h wrote:

Code: Select all

 
$variable = strrpos($string, "-") ? $string : null;
 
Almost.

Code: Select all

$variable = (strpos($string, "-") !== false) ? null : $string;
Post Reply