Take the 2-minute tour ×

Currently I'm using the below snippet, which indent the resulting HTML by using several space characters:

add_filter('get_search_form', 'filter_search_form');

function filter_search_form($form) {
    $form = '      <form role="search" method="get" id="searchform" action="' . home_url('/') . '"><input type="text" placeholder="' . __('Search') . '" value="' . get_search_query() . '" name="s" id="s"><input type="submit" id="searchsubmit" value="' . esc_attr__('Search') . '"></form>' . "\n";
    return $form;

Now I've been reading some about whitespace characters (\t for tab, \n for newline, etc.), but I'm not entirely sure how to implement this in this situation. I've tried using \s for a single space, but without any luck thusfar.

Being relatively new to PHP, I hope you could assist (preferably without using a 'regular' space character).

share|improve this question
Hi! You are a member at Stack Overflow, how come you asked this here? –  Yannis Jan 24 '12 at 18:24
Hi @yannis-rizos, thank you for your reply. To me this looked like a more general question than just [WordPress](wordpress.stackexchange.com), and therefore I'm asking here. –  Cor van Noorloos Jan 24 '12 at 18:28
It's specifically for PHP though so it's probably better off on Stack Overflow –  World Engineer Jan 24 '12 at 18:31
you can read the FAQ for a more in depth description, but generally if the question has code in it and the answer is likely to have code, then it belongs on Stack Overflow or another stack exchange site for more specific types of programming. this site is for more high level topics rather than specific implementation. –  Ryathal Jan 24 '12 at 18:33
I don't understand what you would like to do. Could you please explain better? –  Entropid Jan 24 '12 at 18:43
show 3 more comments

migrated from programmers.stackexchange.com Jan 24 '12 at 18:40


3 Answers

up vote 4 down vote accepted

According to http://php.net/manual/en/regexp.reference.escape.php, the general hexadecimal escape sequence \x20 should work, as should \040 (octal).

Personally, though, I don't see much (if any) benefit to ever specifying spaces in this manner, as it would make your code less readable, IMHO. Just stick literal spaces inside your single- or double-quotes (like you have now) and be done with it.

Alternatively, if you're trying to use whitespace to indent the resulting HTML code (as it seems you are), doing so in units of \t isn't the end of the world.

share|improve this answer
I understand your worries. As an example I've posted the smallest snippet I could find. For a larger snippet however it can definitely benefit on finding things back. –  Cor van Noorloos Jan 24 '12 at 19:21
add comment

Characters like \t and \n need to be in double quotes...

$string = "\t" . '<form></form>';

share|improve this answer
I realize that. If you could show me the space alternative to your reply, you would be more than awesome. –  Cor van Noorloos Jan 24 '12 at 18:47
as far as a I know there is no alternative to a normal space in php...where are you trying to add a space? –  David Nguyen Jan 24 '12 at 18:54
Hi David, I was afraid of that. Basically, to make my code less messy. –  Cor van Noorloos Jan 24 '12 at 19:05
add comment

If you want to insert a tab before it you could use:

$string = "\t" . '<form>....';

(don't forget the double quotes, the single ones don't work with \t, \n and friends!)

If you want spaces, just use spaces!

$string = "        " . '<form>....';

It's html code, so they will be present in the source code of your page. They won't ‘collapsed’ into a single space.

share|improve this answer
Caveat: Using multiple spaces like in PHP like this will put multiple spaces in the resulting HTML code; however, the browser will then "collapse" the spaces in the HTML code into a single space unless you put it in something like a <pre></pre> block. –  Tim Parenti Jan 24 '12 at 19:04
I think that's the point, if I got it right: he wants them in the HTML code and not in the page, just to indent the code. :) –  Entropid Jan 24 '12 at 19:05
Just being thorough, since the usage isn't clear from the question. –  Tim Parenti Jan 24 '12 at 19:09
Thank you anyway for your help then! –  Entropid Jan 24 '12 at 19:09
Hi @entropy, I understand the double quotes. I was looking for a 'space' alternative for tabs \t to make my code more readable. Btw. you don't need to use double quotes for spaces though. Above example works perfectly fine. –  Cor van Noorloos Jan 24 '12 at 19:10
show 2 more comments

Your Answer


