PHP Conference Nagoya 2025

ps_hyphenate

(PECL ps >= 1.1.1)

ps_hyphenateHyphenates a word

说明

ps_hyphenate(resource $psdoc, string $text): array|false

Hyphenates the passed word. ps_hyphenate() evaluates the value hyphenminchars (set by ps_set_value()) and the parameter hyphendict (set by ps_set_parameter()). hyphendict must be set before calling this function.

This function requires the locale category LC_CTYPE to be set properly. This is done when the extension is initialized by using the environment variables. On Unix systems read the man page of locale for more information.

参数

psdoc

Resource identifier of the postscript file as returned by ps_new().

text

text should not contain any non alpha characters. Possible positions for breaks are returned in an array of interger numbers. Each number is the position of the char in text after which a hyphenation can take place.

返回值

An array of integers indicating the position of possible breaks in the text 或者在失败时返回 false.

示例

示例 #1 Hyphennate a text

<?php
$word
= "Koordinatensystem";
$psdoc = ps_new();
ps_set_parameter($psdoc, "hyphendict", "hyph_de.dic");
$hyphens = ps_hyphenate($psdoc, $word);
for(
$i=0; $i<strlen($word); $i++) {
echo
$word[$i];
if(
in_array($i, $hyphens))
echo
"-";
}
ps_delete($psdoc);
?>

以上示例会输出:

Ko-ordi-na-ten-sys-tem

参见

添加备注

用户贡献的备注 1 note

up
1
1manfactory at gmail dot com
8 years ago
The above example does NOT work with German Umlauts (äöü) properly.

I had to do the following to make it work.

setlocale (LC_CTYPE , "de_DE.iso88591"); # it does not work with "de_DE.utf8" and of course your system should run this locale setting

$hyphens = ps_hyphenate($psdoc, utf8_decode($word)); # only our couse if your source code is UTF8 coded

for($i=0; $i<mb_strlen($word); $i++) { # to be on the safe side

echo utf8_encode($word[$i]); # again only if your code is UTF8
To Top