edwardtilbury Posted April 10, 2009 Share Posted April 10, 2009 Here's my custom function.. function parse_title ($text) { $badstuff = array('>', '(', ')', 'discount', 'sale', '/'); $text = str_ireplace('-doors', 'DR', $text); $text = str_ireplace('-door', 'DR', $text); $text = str_ireplace(' front', 'F.', $text); $text = str_ireplace(' rear', 'R.', $text); $text = str_ireplace($badstuff, "", $text); $text = trim ($text); $text = substr ($text, 0, 70); return $text; } I searched around for an intelligent double space remover and found one on this forum.. while (strpos($text,' ') !== false) $str = str_replace(' ',' ',$str); How can I combine the two together as one function? I suppose it some type of combination of brackets or curly braces, but I've tried them and was unsuccessful, I'm probably just putting it in the wrong way. ??? Thanks Link to comment https://forums.phpfreaks.com/topic/153550-remove-double-spaces-and-more-ultimate-data-cleaner-function/ Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Just put $text = str_replace(' ',' ',$text); on the end of your function... Link to comment https://forums.phpfreaks.com/topic/153550-remove-double-spaces-and-more-ultimate-data-cleaner-function/#findComment-806838 Share on other sites More sharing options...
edwardtilbury Posted April 10, 2009 Author Share Posted April 10, 2009 Originally I had five of those lines, but I wanted to make it cleaner and more dynamic. $text = str_replace(' ',' ',$text); This will only delete one double space, but if there is a triple / quadruple space etc it will not delete them. while (strpos($text,' ') !== false) $str = str_replace(' ',' ',$str); This will keep running until it cleans all instances of the double+ spaces, I just wish I could get it to work.. Link to comment https://forums.phpfreaks.com/topic/153550-remove-double-spaces-and-more-ultimate-data-cleaner-function/#findComment-806845 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Ok, well put while (strpos($text,' ') !== false) { $str = str_replace(' ',' ',$str); } on the end of your function then. Link to comment https://forums.phpfreaks.com/topic/153550-remove-double-spaces-and-more-ultimate-data-cleaner-function/#findComment-806847 Share on other sites More sharing options...
edwardtilbury Posted April 10, 2009 Author Share Posted April 10, 2009 <?php function parse_title ($text) { $badstuff = array('>', '(', ')', 'discount', 'sale', '/'); $text = str_ireplace('-doors', 'DR', $text); $text = str_ireplace('-door', 'DR', $text); $text = str_ireplace(' front', 'F.', $text); $text = str_ireplace(' rear', 'R.', $text); $text = str_ireplace($badstuff, "", $text); while (strpos($text,' ') !== false) { $text = str_replace(" ", " ", $text); } $text = trim ($text); $text = substr ($text, 0, 70); return $text; } ?> Woohoo! Final working version, I had some errors in the last one, I wouldn't want anyone to cut and paste a wrong version. Link to comment https://forums.phpfreaks.com/topic/153550-remove-double-spaces-and-more-ultimate-data-cleaner-function/#findComment-806879 Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Sick. Link to comment https://forums.phpfreaks.com/topic/153550-remove-double-spaces-and-more-ultimate-data-cleaner-function/#findComment-806882 Share on other sites More sharing options...
kenrbnsn Posted April 10, 2009 Share Posted April 10, 2009 Use arrays to make this simpler: <?php function parse_title ($text) { $orig = array('-doors','-door',' front',' rear'); $rep = array('DR','DR','F.','R.'); $test = str_ireplace($orig,$rep,$text); $badstuff = array('>', '(', ')', 'discount', 'sale', '/'); $text = str_ireplace($badstuff, '', $text); while (strpos($text,' ') !== false) { $text = str_replace(' ', ' ', $text); } $text = substr (trim($text), 0, 70); return $text; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/153550-remove-double-spaces-and-more-ultimate-data-cleaner-function/#findComment-806897 Share on other sites More sharing options...
thebadbad Posted April 10, 2009 Share Posted April 10, 2009 Instead of using a while loop to remove double spaces, a single run of preg_replace() may be faster: <?php $text = preg_replace('~\s{2,}~', ' ', $text); ?> \s{2,} simply matches 2 or more spaces. Edit: Forgot the single space as replacement. Link to comment https://forums.phpfreaks.com/topic/153550-remove-double-spaces-and-more-ultimate-data-cleaner-function/#findComment-806904 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.