Removing unwanted colons from form field titles

I just created the first poll on my web site and was annoyed by having a colon appear after the question mark in the block title, like this:

Notice the annoying colon right after the question mark.

I remembered that Jeff Robins at Lullabot published an article about how to prevent this problem, but his solution involved copying and modifying the entire theme_form_element function.

In Drupal as with Perl, there is always more than one way to do it. My solution also overrides theme_form_element(), but is far simpler:

function phptemplate_form_element() {
  $args = func_get_args();
  return preg_replace('@([.!?]):\s*(</label>)@i', '$1$2',
      call_user_func_array('theme_form_element', $args));
}

It uses a regular expression to find and remove any colon preceeded by punctuation and followed only by whitespace before the </label>. By using func_get_args() and call_user_func_array(), it also avoids having to know anything about the argument list for theme_form_element(). This code should work for both Drupal 4.7 and 5, though I've only tested it with 5.

Update on June 27, 2007

The above code snippet does not work for required form elements using Drupal 5's default theme because the requiredness-indicator appears in an extra <span> tag between the colon and the </label>. Here is a revised version that works in both cases:

function phptemplate_form_element() {
  $args = func_get_args();
  return preg_replace('@([.!?]):\s*(<span.*?>.*?</span>)?\s*(</label>)@i', '$1$2$3',
      call_user_func_array('theme_form_element', $args));
}

Comments

It's worth noting the core

It's worth noting the core patch for this issue: http://drupal.org/node/67211

I didn't know about that;

I didn't know about that; thanks. However, it's in the 6.x-dev queue and I wanted a solution now. :-)

A nice one, thanks ! The 6th

A nice one, thanks !
The 6th version is still months away, so i would say that as well as a patch as Barry's solution are to be applied.
There are so many developers working on the same problems, so when one developer finds a solution,
the other one may repeat this job without knowing it.

Fantastic little bit of

Fantastic little bit of code! Worked like a charm.

Darn it, Barry, I just came

Darn it, Barry, I just came to post a solution for the required fields and find this update. :)

The Compact Forms module purports to remove trailing colons. I don't think it works yet.

cheers mate, just what i was

cheers mate, just what i was looking for

Hi there! Excuse the silly

Hi there!

Excuse the silly question, but where exactly should this code go? I tried putting it at the bottom of template.php, but the colon is still there.

Any advice you could provide would be greatly appreciated!
Dave

template.php is the correct

template.php is the correct place to put it. If it is not working, use my contact form to send me a snippet of HTML form your site containing a form element with a colon that should have been removed.

nice code. i like this!

nice code. i like this!

I think I've finally found

I think I've finally found someone who cares as much about punctuation and grammar as I do! :D

Excellent. Thankyou! Using

Excellent. Thankyou! Using this to add .classes to theme_form_elements will finally make my forms presentable!

Many thanks for this, Barry.

Many thanks for this, Barry. A compact and elegant solution to an annoying problem. I took this a step further: I don't want any colons or red asterisks on my field labels. The following seems to do the trick:

preg_replace('@(\w):\s*(<span.*?>.*?</span>)?\s*(</label>)@i', '$1$3',
                      call_user_func_array('theme_form_element', $args));

or to remove all colons but keep red asterisks:

preg_replace('@(\w):\s*(<span.*?>.*?</span>)?\s*(</label>)@i', '$1$2$3',
                      call_user_func_array('theme_form_element', $args));

By the way I've used these solutions with Drupal 6.

thanks Barry, will finally

thanks Barry, will finally make my forms presentable!

Andy

good!!

good!!

Really interesting. I am

Really interesting. I am reading the documentation to troubleshoot. Thanks for sharing this. my project is cialis bas prix

Hi Author!Any advice you

Hi Author!Any advice you could provide would be greatly appreciated!Excuse the silly question, but where exactly should this code go? I tried putting it at the bottom of template.php, but the colon is still there.

There are so many developers

There are so many developers working on the same problems, so when one developer finds a solution,the other one may repeat this job without knowing it.The 6th version is still months away, so i would say that as well as a patch as Barry's solution are to be applied.

It was a very nice idea! Just

It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

Interesting way to look at

Interesting way to look at it. For the most part, I agree with you. It would be great if got more post like this. I appreciate it , duloxetine 20 mg.

It uses a regular expression

It uses a regular expression to find and remove any colon preceeded by punctuation and followed only by whitespace before the . By using func_get_args() and call_user_func_array(), it also avoids having to know anything about the argument list for theme_form_element(). This code should work for both Drupal 4.7 and 5, though I've only tested it with 5.

having

having

You will surprise to find the high quality tiffany jewelry in much.Everyone will focus on the shinning of
tiffany co jewelry without awareness of
rovide 100% sterling silver Tiffany jewelry,you can buy discount Tiffany & Co jewelry here.Tiffany And Co Jewelry is the best jewellry.
discount tiffany jewelry, Tiffany Jewelry, Tiffany Jewellery, Tiffany Silver, Tiffany, Tiffanys, Tiffany's Jewelry.We Provide a wide range of fashionable
tiffany co jewelry, including
In 1950 Tiffany's had its biggest boost in popularity as Truman Capote's Breakfast at Tiffany's was published. Tiffany's was shot to worldwide fame in 1961 when the film adaptation staring Audrey Hepburn was released. This film and its star became icons, as did Tiffany's
Tiffany Bracelets
Tiffany Rings
cheap tiffany with.Discounted Tiffany & Co silver jewelries are provided in our Tiffany’s online outlet store
Tiffany Earrings
Tiffany Necklaces
Links Of London
Links Of London

Luckily for us, ed hardy

Luckily for us, ed hardy clothing is one of the few labels that never go out of fashion.
ed hardy
Selling ed hardy men longsleeve,ed hardy swimsuit,ed hardy men swimsuit,ed hardy boot.
ed hardy clothes
ed hardy
Newest ed hardy,ed hardy clothing,Shirts,Swimwear Commodity New styles have just arrived.Cheapest EDHardy Sale.
ed hardy store
ed hardy shoes
best Ed Hardy UK Online Store. We provide customer with Large of high quality and low price Ed Hardy Clothing & better .

online degree | life

That little blue box -

That little blue box - everyone woman dreams of seeing it come their way. tiffany jewelry jewelry is world renowned for its stunning quality and top of the line artisanship; however, it also has a reputation for having a price tag that is way out of most people's leagues.

This totally doesn't work for

This totally doesn't work for Drupal 5!!

I've pasted the funtion into my theme template.php file and when viewing the node (not in edit mode) the colons are still there?!?

Ok, scrap last comment. It

Ok, scrap last comment. It DOES work. But NOT when viewing the node from the front end. What's the point in removing the colon for a form ... but not from the label for users to see!!

How do i remove the colon when viewing the form NOT in edit mode?

For instance here -

http://www.fitforaprincess.co.uk/node/88/

In the grand scheme,

In the grand scheme, vistaprint discount code, gotomypc trial and coupon code are often viewed as being incohate. However, printplace coupon code, printrunner coupon and 123inkjets discount are treated with the utmost respect.

This fashion, worth

This fashion, worth toreplica handbags
replica bags buy.

By 1914, the Louis Vuitton

By 1914, the Louis Vuitton Building opened on the Champs-Elysees. It was the largest Discount Louis Vuitton store in the world at the time. Discount Louis Vuitton also opened in New York, Bombay, Washington, London, Alexandria, and Buenos Aires as World War I began.

Herve Leger womens fashions

Herve Leger womens fashions at ShopStyle. Shop popular stores to find Herve Leger womens fashions on sale - all in one place. Create and share looks based
Herve Leger
Cheap Herve Leger
Jewelry,Necklaces,Jewelry Necklaces,Links of London Necklaces,online sales a variety of world famous such as Links of London etc,with competitive price.
Links of London , the leading British contemporary jeweller was founded in 1990 by jewellery designer Annoushka Ducas and her husband John Ayton.cheap Links of London jewelry at online linksgif UK store, including Links of London Necklaces, Links of London Charms, Links of London Earrings. Links of London Silver Sweetie Bracelet Medium
Links of London Charm
Links of London

I didn't know about that;

I didn't know about that; thanks. However, it's in the 6.x-dev queue and I wanted a solution now. :-)
queue games

wedding dresses wedding

A very nice post.Thanks very

A very nice post.Thanks very much.

Thanks. I'll use this

Thanks. I'll use this information,
jobs for 15 year olds

Hi Author!Any advice you

Hi Author!Any advice you could provide would be greatly appreciated!Excuse the silly question, but where exactly should this code go? I tried putting it at the bottom of template.php, but the colon is still there. free online games

one day i went shopping

one day i went shopping outside,and in an christian louboutin,I found some kinds of christian louboutin i love most they are:christian louboutin christian louboutin cheap christian louboutin cheap christian louboutin christian louboutin on sale christian louboutin on sale christian louboutin discount christian louboutin discount

Chipotle Mexican Grill links

Chipotle Mexican Grill links of london jewelry The fast food at these casual eateries feels slow links of london Rings thanks to organic ingredientsand an emphasis on freshness links of london sale Traffic is down at many locations links of london Necklaces but the company has been able to compensate links london by raising prices links of london Charms and bangle with faceted links of london jewellery claret For converse disguise effete stones links london jewellery Olive Garden links of london Sweetie Bracelets More food for less money hits the sweet spot these days links of london Sweetie Watches sdrwer

It's much easier to use path

It's much easier to use path filter and simply style "internal:node/1" within the href tag. Very nice indeed.

free classified ads |job|steam showers

boys or girls; links of

boys or girls; links of londonWhen in the noon of life ,we are in decision of whether it is meaningful to have links of london salea further education or to search a suitable job, then get a stable salaries, and links of london jewellery when to marry or how to find a good wife or husband who can live through links of london charms the rest of life with us; When in the dusk of life , we are in difficulties to know how to live a happy and sterile life .

Abercrombie clothing shop

Abercrombie clothing shop Abercrombie clothing store Abercrombie clothing store discount Abercrombie Fitch discount Abercrombie Fitch buy Abercrombie and Fitch clothing buy Abercrombie and Fitch clothing Abercrombie and Fitch clothing sale Abercrombie and Fitch clothing sale Ed Hardy Ed Hardy Thank you for the information

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <i> <h1> <h2> <h3> <blockquote>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options