Q:

Regex trim or preg_replace white space including tabs and new lines

How can I use PHP to trim all white space up to "poo" and all white space afterwards?
I would like to turn this:
<code><div class="b-line"></div>  \t 

             \n\n\n \t \n   poo
<lol>
 n \n \n \t </code>

In to this:
<code><div class="b-line"></div>poo 
<lol>
 n</code>

This part will always be at the start of the string: <code><div class="b-line"></div>
Thanks
Edit: Sorry I should explain that the whole of the above is in a string and I only want to trim the whitespace immediately after <code><div class="b-line"></div> and immediately before </code>

A:

$str = trim($str, "\t\n");

See trim

A:

preg_* functions provides whitespace escape sequence \s, which you can use, so you're regex would be:
$regexp = '~...>\\s*([^<]*?)\\s*<~m'

Maybe you will need to use [\\s$] instead of just \\s, I'm nor sure how PCRE handles newlines in those cases.

A:

I think this lookahead and lookbehind based regex will work for you:
$str = <<< EOF
<code><div class="b-line"></div>  \t 

             \n\n\n \t \n   poo
<lol>
 n \n \n \t </code>
EOF;
$str = preg_replace_callback('#(?<=<code><div class="b-line"></div>)(.*?)(\s*<[^>]*>\s*)(.*?)(?=</code>)#is',
       create_function('$m',
       'return str_replace(array("\n", "\t", " "), "", $m[1]).$m[2].str_replace(array("\n", "\t", " "), "", $m[3]);'),
       $str);
var_dump ( $str );

OUTPUT:
string(51) "<code><div class="b-line"></div>poo
<lol>
 n</code>"

