Forum Moderators: coopster

Message Too Old, No Replies

can preg_replace do this in one pass?

to replace white space

         

MisterAoD

6:03 pm on Apr 22, 2006 (gmt 0)

10+ Year Member



I have a little problem wiht regular expression as I stated to study them. My situation is that (well, I use - for tab ;)):


$str = '

<html>
--<head>
----<title>This is it</title>
--</head>
--<body>
----<p>Content goes here</p>--<img src="somepix.jpg" alt="somepix">
--</body
</html>

';

How can I use the preg_replace to replace all white space between tag, at the beginning of the line, and at end of line without affecting content between tag to have a result like this:


<html><head><title>This is it</title></head><body><p>Content goes here</p><img src="somepix.jpg" alt="somepix"></body</html>

Can preg_replace do it in one pass? Currently, my code need two passes to achive this:


$str = preg_replace("/>\s+</","><",$str); //remove between tag
$str = preg_replace("/^\s+([\S\s]+)\s+$/","$1",$str); //remove at the begining and ending of line

Or there is just another method to achive this, your advice is appreciated, thank you.

coopster

8:18 pm on Apr 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, MisterAoD.

I would just trim it.

$str = trim [php.net](preg_replace('/>\s+</','><',$str));

MisterAoD

12:39 am on Apr 23, 2006 (gmt 0)

10+ Year Member



Oh! my goodlord...

Never thought of it

Thank you