Showing posts with label TRIM. Show all posts
Showing posts with label TRIM. Show all posts

Wednesday, 3 August 2011

HTML Super Trim Functions

Remove non breaking spaces and other white space either side of strings

Most languages have a Trim function that removes white space from either side of a string.

However a lot of the time these Trim functions will only remove standard white space e.g if you hit the space bar a couple of times and not other forms of white space such as tabs, new lines or HTML space characters such as non breaking spaces whether they are HTML entity encoded: or Numerically encoded e.g

Therefore sometimes you may need a "Super Trim" function that will handle the removal of all types of space characters including HTML entities.

The following have been written in PHP but can easily be converted into C# or VB. The main part to take away is the regular expression used within each function which replaces a string containing one or more space characters whether they be control characters or HTML entities from either side of the string.

// wrapper function to do trim both sides
function HTMLTrim($text){

// call both functions at once
return HTMLLeftTrim(HTMLRightTrim($text));
}

// removes spaces and at the beginning of strings
function HTMLLeftTrim($text){

// remove space to the left of the text
return preg_replace("@^( | |\s)+(\S+)@","$2",$text);

}

// removes spaces and at the beginning of strings
function HTMLRightTrim($text){

// remove space to the right of the text
return preg_replace("@(\S+)( | |\s)+$@","$1",$text);

}


You can test this out in a simple PHP page with the following code:


$str = "     hello there        ";

echo "before trim its '" . $str . "'";

echo "<br><br>now its '" . HTMLTrim($str) . "'";


Which returns the following output:

before trim its '     hello there      '

now its 'hello there'

I find it very useful when I am scraping content from the web and need to handle the removal of a mixture of standard spaces and HTML spaces.

Monday, 14 March 2011

SQL Varchar Comparison Ignores Right Hand White Space

MS SQL Server Text Comparisons

I was doing some work in SQL 2008 today that involved some data cleaning from a scraper that was extracting specific data from a webpage and trying to match it in a DB. The data was very dirty and not consistent in the slightest so I was running it through a number of custom parser functions to try and find matches.

Whilst doing this I came across something that I certainly thought was odd and hadn't seen before. I don't know if this is a well know "quirk" in MS SQL or not but a couple of colleagues had never seen it either.

It involved string comparisons where one string had white space to the right hand side of it and the other had no space either side and a string comparison matched.

This only occurs when the white space is to the right hand side of the text and not to the left hand side as the following test shows.

I had never come across this quirk before so I thought I would make a note of it incase others hadn't either. I ran this test on 2008 and 2005 with the same results.

DECLARE @Test1 varchar(100),
@Test2 varchar(100),
@Test3 varchar(100)

SELECT @Test1 = 'Stratford ',
@Test2 = ' Stratford',
@Test3 = ' Stratford '

IF @Test1 = 'Stratford'
PRINT 'Is Stratford'
ELSE
PRINT 'Is not Stratford'

IF @Test2 = 'Stratford'
PRINT 'Is Stratford'
ELSE
PRINT 'Is not Stratford'

IF @Test3 = 'Stratford'
PRINT 'Is Stratford'
ELSE
PRINT 'Is not Stratford'

SELECT '"' + @Test1 + '"', '"' + @Test2 + '"', '"' + @Test3 + '"'




The output I get is the following


Is Stratford
Is not Stratford
Is not Stratford

and then a recordset.

"Stratford    " "   Stratford" "   Stratford   "
Maybe this is a well known behaviour but I wasn't expecting it to behave like this so it threw me off a bit during my cleanup process.