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

 

 

 

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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