monkeytooth Posted May 11, 2009 Share Posted May 11, 2009 I know this should be simple but its evading me at the moment, and giving me a headache.. No time to put aside and come back later unfortunately.. So any help would be greatly appreciated. I am outputing a string xxxx-xxxx the x's can be anything or can be "none" I want to run the string through a filter of sorts to find out if it has "none" in it, where if it does then I have to make it re-output the string differently. What would be the simplest solution? would it be exploding it and then checking an array? or is there a simpler tactic? The orginal output is $var1 = xxxx (pulled from DB), $var2 = xxxx (pulled from DB) $var3 = $var1 . "-" . $var2 I have it pull out that way cause I am swapping the database to a new format and in the previous version of the database some of the fields were left blank, null, or "none". In this case I am taking 2 fields and combining them into the $var3 string.. however. If var1 or 2 are blank or none, I want to have the string output the one thats not blank or none twice for sake of what the final outcome is to be later.. or if both are blank I want it to output just none. Link to comment https://forums.phpfreaks.com/topic/157637-finding-var-within-string/ Share on other sites More sharing options...
Axeia Posted May 11, 2009 Share Posted May 11, 2009 The recommended way to find if a string exists in another string is using strpos and read the example on the page, the !== is there for a reason. Then you can manipulate it using one of the many other string manipulation functions, such str_replace Link to comment https://forums.phpfreaks.com/topic/157637-finding-var-within-string/#findComment-831268 Share on other sites More sharing options...
michaellunsford Posted May 11, 2009 Share Posted May 11, 2009 You may have lost me in that last paragraph. But here's a quick try: $var3 = $var1 . "-" . $var2 if(strpos(" ".$var3,"none") OR $var3=="-") echo "none"; You can also target your query to find just the values with 'none' Link to comment https://forums.phpfreaks.com/topic/157637-finding-var-within-string/#findComment-831270 Share on other sites More sharing options...
monkeytooth Posted May 11, 2009 Author Share Posted May 11, 2009 $paystart = $row_jobs['jpayrange']; $payend = $row_jobs['jpayrangee']; if($paystart == "") { $paystart = "none"; } else {} if($payend == "") { $payend = "none"; } else {} $payrange = $paystart . "-" . $payend; if $payrange ends up having "none" in it somewhere, I want to replace which ever spot none was with either start or end Link to comment https://forums.phpfreaks.com/topic/157637-finding-var-within-string/#findComment-831295 Share on other sites More sharing options...
michaellunsford Posted May 11, 2009 Share Posted May 11, 2009 How about an inline shortcut operator? $payrange = ($paystart=="none"?"start":$paystart)."-".($payend=="none"?"end":$payend); Link to comment https://forums.phpfreaks.com/topic/157637-finding-var-within-string/#findComment-831300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.