jeff5656 Posted August 17, 2011 Share Posted August 17, 2011 Let's say I have a variable $x that equals "john smith" and another instance $x equals "Johnson". I want to do something like: IF there is a space between 2 words, THEN $newvariable1 = smith and $newvariable2 = John else: do nothing (i.e. if it's only johnson). How on earth do i do this? :-) Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/ Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 <?php $str = 'John Smith Wilson'; if( ($pos = strpos($str,' ')) !== FALSE ) { // I don't use explode here in case there's 2 or more spaces $first = substr( $str,0,$pos ); $last = substr( $str,$pos+1 ); echo "'$first' '$last'"; } else echo $str; ?> If you aren't worried about multiple spaces, then this code will do, keep in mind any spaces beyond 1 will be lost $str = 'John Smith'; if( strpos($str,' ') !== FALSE ) { list( $first, $last ) = explode( ' ', $str ); echo "'$first' '$last'"; } else echo $str; Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/#findComment-1258754 Share on other sites More sharing options...
dougjohnson Posted August 17, 2011 Share Posted August 17, 2011 You could use "explode". $variable = "John Doe"; $explodedvariable = explode(" ",$variable); $fullname = trim($explodedvariable[0] . " " . $explodedvariable[1]); Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/#findComment-1258756 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 What happens with 'John Doe Wilson' though? Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/#findComment-1258759 Share on other sites More sharing options...
jeff5656 Posted August 17, 2011 Author Share Posted August 17, 2011 Great thanks I will try that. One question. Is it really !== for NOT equal? I always thought it was != (one equal sign not two) Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/#findComment-1258760 Share on other sites More sharing options...
AbraCadaver Posted August 17, 2011 Share Posted August 17, 2011 Great thanks I will try that. One question. Is it really !== for NOT equal? I always thought it was != (one equal sign not two) Strict type checking: 0 == false 0 !== false Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/#findComment-1258761 Share on other sites More sharing options...
Alex Posted August 17, 2011 Share Posted August 17, 2011 You can read more about that and other operators here in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/#findComment-1258764 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 The reason I use !== can be shown in code <?php $var = 0; if( $var == FALSE ) echo 'This will be output'; if( $var === FALSE ) echo 'This will not be output'; ?> The function strpos() returns an offset, which can be 0, or FALSE if the string is not found. For example <?php $var = 'Hello World'; $test = strpos( $var, 'Hello' ); if( $test == FALSE ) echo 'This will be output, even though the string was found'; if( $test === FALSE ) echo 'This will not be output, because the function returned 0, not exactly FALSE'; ?> So, == and != doesn't know the difference between FALSE and 0, or TRUE and a value > 0, while === and !== does. The types of variables compared makes a difference too, when using the EXACT comparison <?php $var = "1"; if( $var == 1 ) echo 'This will be output, even though $var is actually a string'; if( $var === 1 ) echo 'This will not be output, because variable type matters in EXACT comparison'; ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/#findComment-1258765 Share on other sites More sharing options...
DavidAM Posted August 17, 2011 Share Posted August 17, 2011 If you aren't worried about multiple spaces, then this code will do, keep in mind any spaces beyond 1 will be lost . . . Explode accepts an optional third parameter to specify the maximum number of elements to return: $str = 'John Smith Wilson'; if( ($pos = strpos($str,' ')) !== FALSE ) { list( $first, $last ) = explode( ' ', $str , 2 ); // WE ONLY WANT TWO NAME PARTS echo "'$first' '$last'"; } else echo $str; Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/#findComment-1258776 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Did not know that about explode. DavidAM has the best solution. Quote Link to comment https://forums.phpfreaks.com/topic/245068-detect-a-space-in-a-variable/#findComment-1258794 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.