Jeffro Posted May 10, 2011 Share Posted May 10, 2011 I sometimes have words thrown into my database with the following symbol: … What I'm trying to achieve is this.. Anytime that symbol is found, replace it and all previous letters.. back to the first space encountered... with nothing. Make sense? If I had the sentence "the lazy brown d..… , it should return for me "the lazy brown" If I was just replacing the weird code, it would be a simple str_replace, but not sure how to make the letters before the symbol (up to the first space) go away. Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/236048-how-do-i-str_replace-this/ Share on other sites More sharing options...
dawsba Posted May 11, 2011 Share Posted May 11, 2011 <?php $search_chars = array(); for($i=128;$i<=255;$i++){array_push($search_chars,$i);} $string = "the lazy brown d..…"; $orig = $string; function strpos_chararray($haystack, $needles) { if(is_array($needles)) { foreach($needles as $key => $str) { if(is_numeric($str)){$str = chr($str);} if(is_array($str)) { $pos = strpos_array($haystack, $str); } else { $pos = strpos($haystack, $str); } if ($pos !== FALSE) { return $pos; } } if ($pos === FALSE) { return FALSE; } } else { return strpos($haystack, $needles); } } function rebuild($string) { global $search_chars; $str = explode(' ',$string); foreach($str as $str_bit) { $chk = strpos_chararray($str_bit, $search_chars); if($chk === FALSE) { $new_str .= ' '.$str_bit; } else{break;} } return $new_str; } if(strpos_chararray($string, $search_chars)) { $string = rebuild($string); } echo "ORIGINAL >> ".$orig."<br>"; echo "NEW >> ".$string."<br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/236048-how-do-i-str_replace-this/#findComment-1213509 Share on other sites More sharing options...
Jeffro Posted May 12, 2011 Author Share Posted May 12, 2011 Hmmm... can't seem to make this work. Quote Link to comment https://forums.phpfreaks.com/topic/236048-how-do-i-str_replace-this/#findComment-1214292 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.