Hi
I'm trying to write a thing to replace any number of spaces between two words with a <span> of the equivalent width - though obviously a lot depends on the font etc.
the user types words into a big textarea and uses the space bar to position them on a line
the text is then sent to the php page that searches through for spaces and replaces them
at the moment I've got this
$str = $_SESSION['txt'];
$str = str_replace("\r", "<br§/>", $str);
$str = str_replace(' ', '<span§style="padding-left:3em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:2.5em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:2em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:1.5em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:1em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:0.5em"></span>', $str);
$str = str_replace("§", " ", $str);
echo $str;
the problem with this is that if there are 30 spaces between two words then it will replace them with
<span style="padding-left:3em"></span><span style="padding-left:3em"></span><span style="padding-left:3em"></span><span style="padding-left:3em"></span><span style="padding-left:3em"></span>
so there must be a simple way of counting the spaces between words and replacing with a span with padding-left that is the number of spaces * 0.5
here's the whole code :
<? session_start();
$_SESSION['txt'] = $_POST['txt'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
body{
font-family:Arial, Georgia, "Times New Roman", Times, serif;
font-size:1em;
}
</style>
</head>
<body>
<form action="test-spaces.php" method="post">
<textarea name="txt" style="width:700px;height:300px;font-family:Arial, Georgia, Times, serif;font-size:1em;"><? echo $_SESSION['txt']; ?></textarea><br />
<input type="submit" name="_SEND" value="send" />
</form>
<?
$str = $_SESSION['txt'];
$str = str_replace("\r", "<br§/>", $str);
$str = str_replace(' ', '<span§style="padding-left:3em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:2.5em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:2em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:1.5em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:1em"></span>', $str);
$str = str_replace(' ', '<span§style="padding-left:0.5em"></span>', $str);
$str = str_replace("§", " ", $str);
echo $str;
?>
</body>
</html>
has anyone got any ideas how i can do this ?
thanks