<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: strings code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 10:43:45 GMT</pubDate>
    <description>DZone Snippets: strings code</description>
    <item>
      <title>Ruby Title Case</title>
      <link>http://snippets.dzone.com/posts/show/4702</link>
      <description>Capitalize all words in a string with the briefest of snippets!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;'some string here'.gsub(/\b\w/){$&amp;.upcase}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 27 Oct 2007 00:41:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4702</guid>
      <author>eliazar (eliazar parra)</author>
    </item>
    <item>
      <title>String equality tester</title>
      <link>http://snippets.dzone.com/posts/show/4253</link>
      <description>If you want to test the equality of two strings and don't want the overhead of strcmp(), then this is the function for you.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;_Bool strequals(char* a, char* b) {&lt;br /&gt; if (!a || !b) return 0;&lt;br /&gt; do {if (*a != *b) return 0; } while (*a++ &amp;&amp; *b++);&lt;br /&gt; return 1;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:22:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4253</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>create an array or split a long string seperated by tabs newlines carriage returns and commas.</title>
      <link>http://snippets.dzone.com/posts/show/4120</link>
      <description>create an array or split a long string seperated by tabs newlines carriage returns and commas.&lt;br /&gt;&lt;br /&gt;EG&lt;br /&gt;&lt;br /&gt;emails = "skdjf@sdklfj.com sdflj@kj.com sldfj@klj.com, slkj@kjl.com,lskjdf@lkj.com, \r\nlkjkjl@kjk.com"&lt;br /&gt;&lt;br /&gt;becomes&lt;br /&gt;&lt;br /&gt;=&gt; ["skdjf@sdklfj.com", "sdflj@kj.com", "sldfj@klj.com", "slkj@kjl.comlskjdf@lkj.com", "lkjkjl@kjk.com"]&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;emails = emails.split(/,/).join().split()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 09 Jun 2007 00:48:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4120</guid>
      <author>mcarr (matt)</author>
    </item>
    <item>
      <title>Actionscript _String Class</title>
      <link>http://snippets.dzone.com/posts/show/3512</link>
      <description>&lt;code&gt;dynamic class _String {&lt;br /&gt;	// Replace a string or substrings within a string&lt;br /&gt;	static function Replace (the_String, search_String, replace_String, occurrences, backward) {&lt;br /&gt;		if (search_String == replace_String) return the_String;&lt;br /&gt;		var found = 0;&lt;br /&gt;		if (backward == true) {&lt;br /&gt;			var pos = the_String.lastIndexOf(search_String);&lt;br /&gt;			while (pos&gt;= 0) {&lt;br /&gt;				found++;&lt;br /&gt;				var start_String = the_String.substr(0, pos);&lt;br /&gt;				var end_String = the_String.substr(pos + search_String.length);&lt;br /&gt;				the_String = start_String + replace_String + end_String;&lt;br /&gt;				pos = the_String.lastIndexOf(search_String, start_String.length);&lt;br /&gt;				if (found == occurrences) pos = -1;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		else {&lt;br /&gt;			var pos = the_String.indexOf(search_String);&lt;br /&gt;			while (pos&gt;= 0) {&lt;br /&gt;				found++;&lt;br /&gt;				var start_String = the_String.substr(0, pos);&lt;br /&gt;				var end_String = the_String.substr(pos + search_String.length);&lt;br /&gt;				the_String = start_String + replace_String + end_String;&lt;br /&gt;				pos = the_String.indexOf(search_String, pos + replace_String.length);&lt;br /&gt;				if (found == occurrences) pos = -1;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		return the_String;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	// Convert delimited (comma by default) String to an Array&lt;br /&gt;	static function toArray(string, separator:String) {&lt;br /&gt;		var list = new Array();&lt;br /&gt;		if (typeof(string) == "string"){&lt;br /&gt;			if (separator == undefined) separator = ",";&lt;br /&gt;			if (string == null) return false;&lt;br /&gt;			var currentStringPosition = 0;&lt;br /&gt;			while (currentStringPosition&lt;string.length) {&lt;br /&gt;				var nextIndex = string.indexOf(separator, currentStringPosition);&lt;br /&gt;				if (nextIndex == -1) break;&lt;br /&gt;				var word = string.slice(currentStringPosition, nextIndex);&lt;br /&gt;				list.push(word);&lt;br /&gt;				currentStringPosition = nextIndex+1;&lt;br /&gt;			}&lt;br /&gt;			if (list.length&lt;1) list.push(string);&lt;br /&gt;			else list.push(string.slice(currentStringPosition, string.length));&lt;br /&gt;		} else {&lt;br /&gt;			list.push(string);&lt;br /&gt;		}&lt;br /&gt;		return list;&lt;br /&gt;	}&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Tue, 13 Feb 2007 19:19:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3512</guid>
      <author>bgidge (Bryan Gidge)</author>
    </item>
    <item>
      <title>Convert String-Type the C++-Way</title>
      <link>http://snippets.dzone.com/posts/show/3310</link>
      <description>A very useful snipped that converts strings to/from various types.&lt;br /&gt;&lt;br /&gt;Here's the header:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;string&gt;&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;#include &lt;vector&gt;&lt;br /&gt;#include &lt;sstream&gt;&lt;br /&gt;#include &lt;iomanip&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;#ifndef ST_STRING&lt;br /&gt;#define ST_STRING&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;enum CONVTYPE { STRTOINT, STRTOFLOAT, INTTOSTR, STRTODOUBLE }; &lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; *  Convtype converts an integer to a string and a string &lt;br /&gt; *  string to an integer, a float, or a double. The third argument&lt;br /&gt; *  of this method determines what to do:&lt;br /&gt; *  STRTOINT, STRTOFLOAT, INTTOSTR, and STRTODOUBLE.&lt;br /&gt; *&lt;br /&gt; */&lt;br /&gt;void convtype(void*, void*, const int&amp; );  &lt;br /&gt;  &lt;br /&gt;#endif&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The appropiate implementation:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;void convtype(void* inp, void* out, const int&amp; convtype)&lt;br /&gt;{&lt;br /&gt;  istringstream isbuf;&lt;br /&gt;  ostringstream osbuf;&lt;br /&gt;&lt;br /&gt;  switch(convtype)&lt;br /&gt;    {&lt;br /&gt;    case STRTOINT:&lt;br /&gt;      isbuf.str(*(string*) inp);&lt;br /&gt;      isbuf &gt;&gt; *((int*) out);&lt;br /&gt;      break;&lt;br /&gt;    case STRTOFLOAT:&lt;br /&gt;      isbuf.str(*(string*) inp);&lt;br /&gt;      isbuf &gt;&gt; *((float*) out);&lt;br /&gt;      break;&lt;br /&gt;    case STRTODOUBLE:&lt;br /&gt;      isbuf.str(*(string*) inp);&lt;br /&gt;      isbuf &gt;&gt; *((double*) out);&lt;br /&gt;      break;&lt;br /&gt;    case INTTOSTR:&lt;br /&gt;      osbuf &lt;&lt; *((int*) inp);&lt;br /&gt;      *((string*)out)=osbuf.str();&lt;br /&gt;      break;&lt;br /&gt;    }&lt;br /&gt;  return;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 17 Jan 2007 20:52:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3310</guid>
      <author>stayefeh (Sascha Tayefeh)</author>
    </item>
    <item>
      <title>Python String Breaking and Beginners Reg Ex All-in-One !</title>
      <link>http://snippets.dzone.com/posts/show/2731</link>
      <description>http://mail.python.org/pipermail/python-list/2002-October/125367.html&lt;br /&gt;&lt;br /&gt;// I found this on a python mailling list site&lt;br /&gt;&lt;br /&gt;Ken wrote:&lt;br /&gt;&gt; "Padraig Brady" &lt;Padraig@Linux.ie&gt; wrote in message&lt;br /&gt;&gt; news:3D9AFA69.2020804@Linux.ie...&lt;br /&gt;&gt; &lt;br /&gt;&gt;&gt;Ken wrote:&lt;br /&gt;&gt;&gt;&lt;br /&gt;&gt;&gt;&gt;Hi all, I am trying to do a simple word search engine. Is there an easy&lt;br /&gt;&gt;&gt;&lt;br /&gt;&gt; way&lt;br /&gt;&gt; &lt;br /&gt;&gt;&gt;&gt;to break up a sentence into individual words so that I can use it to&lt;br /&gt;&gt;&gt;&lt;br /&gt;&gt; compare&lt;br /&gt;&gt; &lt;br /&gt;&gt;&gt;&gt;without traversing through every character?&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&gt;&gt;&gt;Eg, something like this:&lt;br /&gt;&gt;&gt;&gt;from: "This is an example"&lt;br /&gt;&gt;&gt;&gt;to: ["This", "is", "an", "example"]&lt;br /&gt;&gt;&gt;&lt;br /&gt;&gt;&gt;You can use &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;"".split()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;but that will not&lt;br /&gt;&gt;&gt;deal with punctuation. For that you will&lt;br /&gt;&gt;&gt;need re.&lt;br /&gt;&gt;&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import re&lt;br /&gt;re.split('\W+', "This is an, example.")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&gt;&gt;&lt;br /&gt;&gt;&gt;This however will create an empty list item&lt;br /&gt;&gt;&gt;for the last '.'&lt;br /&gt;&gt;&gt;&lt;br /&gt;&gt;&gt;You can get around this by using the converse:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;re.findall('\w+', "This is an, example.")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&gt;&gt;&lt;br /&gt;&gt;&gt;P&#225;draig.&lt;br /&gt;&gt; &lt;br /&gt; Does string.rstrip() get rid of the commas, fullstops etc.?&lt;br /&gt;&lt;br /&gt;Don't get mixed up between strip and split.&lt;br /&gt;The help for rstrip says "returns a string with trailing whitespace removed".&lt;br /&gt;I.E. "123  " -&gt; "123" however "123   ." doesn't change.&lt;br /&gt;&lt;br /&gt;&gt; Also, can you explain what the parameters "re.findall('\w+', "This is an,&lt;br /&gt;&gt; example.")" mean?&lt;br /&gt;&lt;br /&gt;The \w+ is a regular expression that matches one or more&lt;br /&gt;characters in the set [a-zA-Z0-9_]. I.E. it matches words.&lt;br /&gt;Anything else (like spaces, punctuation) is not returned.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import re&lt;br /&gt;mystring="This is an, example."&lt;br /&gt;re.findall(r'\w+', mystring)     #all words&lt;br /&gt;re.findall(r'\w*i\w*', mystring) #all words containing letter i&lt;br /&gt;re.findall(r'\w{4,}', mystring)  #all words &gt;= 4 letters&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;P&#225;draig.&lt;br /&gt;</description>
      <pubDate>Fri, 29 Sep 2006 19:09:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2731</guid>
      <author>Williscool (William Harris)</author>
    </item>
    <item>
      <title>Format strings like PRINT USING, for Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2472</link>
      <description>This is an extension to Ruby's String class. Just put this code in a file and include it. This basically takes a string and adds literals and padding to it. For example, you can format a phone number like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;"5445556747".using('(###) ###-####', '', true)&lt;br /&gt;   =&gt; (544) 555-6747&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'strscan'&lt;br /&gt;&lt;br /&gt;class String&lt;br /&gt;&lt;br /&gt;  # Returns the string formatted according to a pattern.&lt;br /&gt;  #&lt;br /&gt;  # The pattern consists of placeholders and literals. The string is placed in&lt;br /&gt;  # the placeholders, leaving the literals as they are. The result may be&lt;br /&gt;  # truncated or padded if there are more placeholders than strings.&lt;br /&gt;  #&lt;br /&gt;  # Placeholders are '#' or '&amp;'. Each '#' is replaced by one character from&lt;br /&gt;  # the string, or the filler character if the string has no characters left.&lt;br /&gt;  # The '&amp;' is replaced by any remaining characters, or left out of the result&lt;br /&gt;  # if there are no remaining characters. There can only be one '&amp;' in the&lt;br /&gt;  # pattern. If there is no '&amp;', remaining characters are discarded.&lt;br /&gt;  # &lt;br /&gt;  # '#' or '&amp;' may be replaced by other characters if they are needed as&lt;br /&gt;  # literals.&lt;br /&gt;  #&lt;br /&gt;  # Examples:&lt;br /&gt;  # "123456789".using('###-##-####')&lt;br /&gt;  #    =&gt; "123-45-6789"&lt;br /&gt;  # "12345".using('###-##-####')&lt;br /&gt;  #    =&gt; "123-45"&lt;br /&gt;  # "12345".using('###-##-####', nil)&lt;br /&gt;  #    =&gt; "12345"&lt;br /&gt;  # "12345".using('###-##-####', ' ')&lt;br /&gt;  #    =&gt; "123-45-    "&lt;br /&gt;  # "5551212".using ('(###) ###-####', '', true)&lt;br /&gt;  #    =&gt; "555-1212"&lt;br /&gt;  # "873555121276668".using ('(###) ###-#### ext &amp;', '', true)&lt;br /&gt;  #    =&gt; "(873) 555-1212 ext 76668"&lt;br /&gt;  # "KB5774X".using ('##-&amp;-#')&lt;br /&gt;  #    =&gt; "KB-5774-X"&lt;br /&gt;  #&lt;br /&gt;  # Parameters:&lt;br /&gt;  # pattern -- The format string, see above.&lt;br /&gt;  # fill    -- A string for padding. If the empty string, then the pattern is&lt;br /&gt;  #            filled as much as possible, and the rest of the pattern is&lt;br /&gt;  #            truncated. If nil, and the string does not fill the pattern,&lt;br /&gt;  #            the string is returned unchanged.  Otherwise, the string is&lt;br /&gt;  #            padded to fill the pattern, which is not truncated. Defaults to&lt;br /&gt;  #            the empty string.&lt;br /&gt;  # right   -- If true, the pattern is filled from right-to-left instead of&lt;br /&gt;  #            from left-to-right, and truncated on the left instead of the&lt;br /&gt;  #            right if needed. Default is false.&lt;br /&gt;  # fixchar -- The single-character placeholder. Default is '#'.&lt;br /&gt;  # remchar -- The remaining-character placeholder. Default is '&amp;'.&lt;br /&gt;  #&lt;br /&gt;  def using(pattern, fill='', right=false, fixchar='#', remchar='&amp;')&lt;br /&gt;&lt;br /&gt;    remCount = pattern.count(remchar)&lt;br /&gt;    raise ArgumentError.new("Too many #{remchar}") if remCount &gt; 1&lt;br /&gt;    raise ArgumentError.new("#{fixchar} too long") if fixchar.length &gt; 1&lt;br /&gt;    raise ArgumentError.new("#{remchar} too long") if remchar.length &gt; 1&lt;br /&gt;    raise ArgumentError.new("#{fill} too long")    if fill.length &gt; 1&lt;br /&gt;    remaining = remCount != 0&lt;br /&gt;    slots = pattern.count(fixchar)&lt;br /&gt;&lt;br /&gt;    # Return the string if it doesn't fit and we shouldn't even try,&lt;br /&gt;    if fill.nil?&lt;br /&gt;      return self if self.length &lt; slots&lt;br /&gt;      return self if self.length &gt; slots and !remaining&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # Pad and clone the string if necessary.&lt;br /&gt;    source =  if fill.nil? || fill.empty? then&lt;br /&gt;                self&lt;br /&gt;              elsif right then&lt;br /&gt;                self.rjust(slots, fill)&lt;br /&gt;              else&lt;br /&gt;                self.ljust(slots, fill)&lt;br /&gt;              end&lt;br /&gt;    &lt;br /&gt;    # Truncate the string if necessary.&lt;br /&gt;    if source.length &gt; slots &amp;&amp; !remaining then&lt;br /&gt;      source = right ? source[-source.length, source.length] :&lt;br /&gt;                       source[0, source.length]&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # Truncate pattern if needed.&lt;br /&gt;    if !fill.nil? &amp;&amp; fill.empty? then&lt;br /&gt;      &lt;br /&gt;      if source.length &lt; slots  # implies '&amp;' can be ignored&lt;br /&gt;        keepCount = source.length # Number of placeholders we are keeping&lt;br /&gt;        leftmost, rightmost = 0, pattern.length - 1&lt;br /&gt;        if right then&lt;br /&gt;          # Look right-to-left until we find the last '#' to keep.&lt;br /&gt;          # Loop starts at 1 because 0th placeholder is in the inject param.&lt;br /&gt;          leftmost = (1...keepCount).inject(pattern.rindex(fixchar)) {&lt;br /&gt;            |leftmost, n| pattern.rindex(fixchar, leftmost - 1) }&lt;br /&gt;        else&lt;br /&gt;          # Look left-to-right until we find the last '#' to keep.&lt;br /&gt;          rightmost = (1...keepCount).inject(pattern.index(fixchar)) {&lt;br /&gt;            |rightmost, n| pattern.index(fixchar, rightmost + 1) }&lt;br /&gt;        end&lt;br /&gt;        pattern = pattern[leftmost..rightmost]&lt;br /&gt;        slots = pattern.count(fixchar)&lt;br /&gt;      end&lt;br /&gt;    &lt;br /&gt;      # Trim empty '&amp;' up to nearest placeholder. If a '&amp;' goes empty, the&lt;br /&gt;      # literals between it and the nearest '#' are probably also unnecessary.&lt;br /&gt;      if source.length == slots then&lt;br /&gt;        if pattern.match("^#{Regexp.escape(remchar)}") then&lt;br /&gt;          pattern = pattern[pattern.index(fixchar) || 0 ... pattern.length]&lt;br /&gt;        elsif pattern.match("#{Regexp.escape(remchar)}$") then&lt;br /&gt;          pattern = pattern[0 ... (pattern.rindex(fixchar) + fixchar.length) || pattern.length]&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;    end&lt;br /&gt;      &lt;br /&gt;    # Figure out how long the remainder will be when we get to it.&lt;br /&gt;    remSize = source.length - slots&lt;br /&gt;    if remSize &lt; 0 then remSize = 0; end&lt;br /&gt;    &lt;br /&gt;    # Make the result.&lt;br /&gt;    scanner = ::StringScanner.new(pattern)&lt;br /&gt;    sourceIndex = 0&lt;br /&gt;    result = ''&lt;br /&gt;    fixRegexp = Regexp.new(Regexp.escape(fixchar))&lt;br /&gt;    remRegexp = Regexp.new(Regexp.escape(remchar))&lt;br /&gt;    while not scanner.eos?&lt;br /&gt;      if scanner.scan(fixRegexp) then&lt;br /&gt;        result += source[sourceIndex].chr&lt;br /&gt;        sourceIndex += 1&lt;br /&gt;      elsif scanner.scan(remRegexp) then&lt;br /&gt;        result += source[sourceIndex, remSize]&lt;br /&gt;        sourceIndex += remSize&lt;br /&gt;      else&lt;br /&gt;        result += scanner.getch&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    result&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 26 Aug 2006 19:21:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2472</guid>
      <author>Agent (Dustin Voss)</author>
    </item>
    <item>
      <title>Text to HTML converter (PHP 4+)</title>
      <link>http://snippets.dzone.com/posts/show/2223</link>
      <description>Simple function to convert a text into formatted HTML in PHP. The function implements some text cleanups (double space removal) and accepts &lt;b&gt;some&lt;/b&gt; HTML in the text, like links (a href), lists (ul, ol), blockquotes and tables. This makes it perfect for use inside custom-made blogging engines and CMSs. There's also an implementation of case-insensitive search/replace for php &lt; 5.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;function stri_replace( $find, $replace, $string ) {&lt;br /&gt;// Case-insensitive str_replace()&lt;br /&gt;&lt;br /&gt;  $parts = explode( strtolower($find), strtolower($string) );&lt;br /&gt;&lt;br /&gt;  $pos = 0;&lt;br /&gt;&lt;br /&gt;  foreach( $parts as $key=&gt;$part ){&lt;br /&gt;    $parts[ $key ] = substr($string, $pos, strlen($part));&lt;br /&gt;    $pos += strlen($part) + strlen($find);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return( join( $replace, $parts ) );&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;function txt2html($txt) {&lt;br /&gt;// Transforms txt in html&lt;br /&gt;&lt;br /&gt;  //Kills double spaces and spaces inside tags.&lt;br /&gt;  while( !( strpos($txt,'  ') === FALSE ) ) $txt = str_replace('  ',' ',$txt);&lt;br /&gt;  $txt = str_replace(' &gt;','&gt;',$txt);&lt;br /&gt;  $txt = str_replace('&lt; ','&lt;',$txt);&lt;br /&gt;&lt;br /&gt;  //Transforms accents in html entities.&lt;br /&gt;  $txt = htmlentities($txt);&lt;br /&gt;&lt;br /&gt;  //We need some HTML entities back!&lt;br /&gt;  $txt = str_replace('&amp;quot;','"',$txt);&lt;br /&gt;  $txt = str_replace('&amp;lt;','&lt;',$txt);&lt;br /&gt;  $txt = str_replace('&amp;gt;','&gt;',$txt);&lt;br /&gt;  $txt = str_replace('&amp;amp;','&amp;',$txt);&lt;br /&gt;&lt;br /&gt;  //Ajdusts links - anything starting with HTTP opens in a new window&lt;br /&gt;  $txt = stri_replace("&lt;a href=\"http://","&lt;a target=\"_blank\" href=\"http://",$txt);&lt;br /&gt;  $txt = stri_replace("&lt;a href=http://","&lt;a target=\"_blank\" href=http://",$txt);&lt;br /&gt;&lt;br /&gt;  //Basic formatting&lt;br /&gt;  $eol = ( strpos($txt,"\r") === FALSE ) ? "\n" : "\r\n";&lt;br /&gt;  $html = '&lt;p&gt;'.str_replace("$eol$eol","&lt;/p&gt;&lt;p&gt;",$txt).'&lt;/p&gt;';&lt;br /&gt;  $html = str_replace("$eol","&lt;br /&gt;\n",$html);&lt;br /&gt;  $html = str_replace("&lt;/p&gt;","&lt;/p&gt;\n\n",$html);&lt;br /&gt;  $html = str_replace("&lt;p&gt;&lt;/p&gt;","&lt;p&gt;&amp;nbsp;&lt;/p&gt;",$html);&lt;br /&gt;&lt;br /&gt;  //Wipes &lt;br&gt; after block tags (for when the user includes some html in the text).&lt;br /&gt;  $wipebr = Array("table","tr","td","blockquote","ul","ol","li");&lt;br /&gt;&lt;br /&gt;  for($x = 0; $x &lt; count($wipebr); $x++) {&lt;br /&gt;&lt;br /&gt;    $tag = $wipebr[$x];&lt;br /&gt;    $html = stri_replace("&lt;$tag&gt;&lt;br /&gt;","&lt;$tag&gt;",$html);&lt;br /&gt;    $html = stri_replace("&lt;/$tag&gt;&lt;br /&gt;","&lt;/$tag&gt;",$html);&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return $html;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 20 Jun 2006 23:01:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2223</guid>
      <author>fzero (Fabio FZero)</author>
    </item>
    <item>
      <title>Capitalizing titles in Ruby - another take...</title>
      <link>http://snippets.dzone.com/posts/show/557</link>
      <description>This code was inspired by Peter's code from May 17... &lt;br /&gt;&lt;br /&gt;I basically extended his idea to be more flexible and added ! methods to modify the string in place. I chose to use a different regex selector because using "\b" breaks on apostrophes and leave you with words like "You'Re" instead of "You're"...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class String&lt;br /&gt;&lt;br /&gt;  def titlecase()&lt;br /&gt;    ignore_list = %w{of etc and by the for on is at to but nor or a via}&lt;br /&gt;    capitalize_all_ex(ignore_list)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def titlecase!()&lt;br /&gt;    ignore_list = %w{of etc and by the for on is at to but nor or a via}&lt;br /&gt;    capitalize_all_ex!(ignore_list)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all(force_downcase = true)&lt;br /&gt;    ignore_list = %w{}&lt;br /&gt;    capitalize_all_ex(ignore_list, force_downcase)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all!(force_downcase = true)&lt;br /&gt;    ignore_list = %w{}&lt;br /&gt;    capitalize_all_ex!(ignore_list, force_downcase)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all_ex(ignore_list, force_downcase = true)&lt;br /&gt;    # if force_downcase is true then the &lt;br /&gt;    # string is, um, downcased first :-)&lt;br /&gt;    if force_downcase&lt;br /&gt;      self.downcase.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      }&lt;br /&gt;    else&lt;br /&gt;      self.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      }&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all_ex!(ignore_list, force_downcase = true)&lt;br /&gt;    if force_downcase&lt;br /&gt;      self.replace(self.downcase.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      })&lt;br /&gt;    else&lt;br /&gt;      self.replace(self.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      })&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sat, 06 Aug 2005 00:09:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/557</guid>
      <author>s0nspark (Tim Ferrell)</author>
    </item>
    <item>
      <title>Truncate String by Words</title>
      <link>http://snippets.dzone.com/posts/show/412</link>
      <description>This will take a phrase and truncate it at the word level&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;function trunc($phrase, $max_words)&lt;br /&gt;{&lt;br /&gt;   $phrase_array = explode(' ',$phrase);&lt;br /&gt;   if(count($phrase_array) &gt; $max_words &amp;&amp; $max_words &gt; 0)&lt;br /&gt;      $phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'...'  &lt;br /&gt;   return $phrase;&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 26 Jun 2005 02:18:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/412</guid>
      <author>banderson623 (Brian Anderson)</author>
    </item>
  </channel>
</rss>
