Jump to content

Remove double spaces and more, ultimate data cleaner function..


edwardtilbury

Recommended Posts

Here's my custom function.. :P

 

 

 

		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

 

 

 

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..

 

<?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.

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.