refiking Posted April 22, 2009 Share Posted April 22, 2009 Ok. How do I do this? I need to add abc to the string if it isn't already there. I already used strpos to see if it is there. Here is the current code: $pref = "abc"; $ie = strpos($order,$pref); IF ($ie == false){ // $new = add abc before the 167 values } ?> Link to comment https://forums.phpfreaks.com/topic/155246-i-need-to-add-characters-to-a-string/ Share on other sites More sharing options...
DjMikeS Posted April 22, 2009 Share Posted April 22, 2009 Do you need to add the characters to the beginning of the string ? If so, you could do: <?php $pref = "abc"; $ie = strpos($order,$pref); IF ($ie == false){ $new = 'abc' . $order; } ?> Link to comment https://forums.phpfreaks.com/topic/155246-i-need-to-add-characters-to-a-string/#findComment-816765 Share on other sites More sharing options...
sKunKbad Posted April 22, 2009 Share Posted April 22, 2009 $pref = "abc"; $ie = strpos($order,$pref); IF ($ie == false){ $new = $pref .= $order; } Link to comment https://forums.phpfreaks.com/topic/155246-i-need-to-add-characters-to-a-string/#findComment-816766 Share on other sites More sharing options...
Zhadus Posted April 22, 2009 Share Posted April 22, 2009 Actually, you'll need to use: <?php $pref = "abc"; $ie = strpos($order,$pref); IF ($ie === false){ $new = $pref .= $order; } ?> If you just use ==, and 'abc' is the first 3 characters, strpos will return a 0 which is == to false, but not === to false. EDIT: Bad code tag. Link to comment https://forums.phpfreaks.com/topic/155246-i-need-to-add-characters-to-a-string/#findComment-816792 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.