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 Quote Link to comment 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... Quote Link to comment 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.. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 10, 2009 Share Posted April 10, 2009 Sick. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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.