mirror of
https://github.com/clagnut/webtypography.git
synced 2025-09-24 20:01:34 +02:00
14 lines
1.9 KiB
HTML
14 lines
1.9 KiB
HTML
<blockquote class='quote-from-book'> <p><span class='ic'>“</span>Hard spaces are useful for preventing line-breaks within phrases such as <em>6.2 mm</em>, <em>3 in.</em>, <em>4 × 4</em>, or in phrases like <em>page 3</em> and <em>chapter 5</em>.”</p>
|
|
</blockquote>
|
|
<p>In <abbr title="HyperText Mark-up Language">HTML</abbr> the hard space character is known as a non-breaking space and is inserted using the entity <code>&nbsp;</code>, for example:</p>
|
|
|
|
<pre><code>1&nbsp;inch is equivalent to 2.54&nbsp;cm.</code></pre>
|
|
|
|
<p>It should not really be expected of authors to type <code>&nbsp;</code> everytime a non-breaking space is required. This is a job that, to some degree, can be performed automatically by a content management system. To demonstrate this by way of simple <abbr title="PHP HyperText Processor">PHP</abbr> example, a regular expression function could automatically insert non-breaking spaces as follows:</p>
|
|
|
|
<pre><code>$search = '/<span class='bracket'>(</span><span class='bracket'>[</span>0-9<span class='bracket'>]</span><span class='bracket'>)</span> <span class='bracket'>(</span><span class='bracket'>[</span>a-zA-Z<span class='bracket'>]</span><span class='bracket'>)</span>/';
|
|
$replace = '$1&nbsp;$2';
|
|
$text = preg_replace<span class='bracket'>(</span>$search,$replace,$text<span class='bracket'>)</span>;</code></pre>
|
|
|
|
<p>This function looks for sequences of a number followed by a space followed by a letter and replaces the space with a non-breaking space. This would deal with instances such as <em>2.54 cm</em>. For phrases like <em>chapter 5</em> it would be better to create a set of watch-words rather than attaching every number to a preceding string. Additionally rules would need to be created to handle common mathematical operators as in <em>4 × 4</em>.</p>
|
|
|