dポイントプレゼントキャンペーン実施中!

改行されていない英文を取得した際に、ピリオドで改行しようとしました。
PHPにて
$dot_pattern = "|\.{1}|i";
preg_replace($dot_pattern, ".\n", $result);
のようにしたら、例えばU.S.Aなどでも改行してしまいます。

これを、各文の終わりのみ改行したいのですが、どのようにすればいいのでしょうか?
色々と考えてみましたが、自分には無理でした。

まぁ、英文ではおそらく文末以外では大文字の後にピリオドが来るとは思うのですが。

すみませんがアドバイスを頂けないでしょうか?

A 回答 (3件)

曖昧な条件では無理なので、「直前が英小文字であるピリオド」を対象にするなら、



preg_replace("|([a-z])\.|","\1.\n"、$result)

「直前が英大文字でないピリオド」だと、"xx....." みたいなケースでそれぞれ改行されてしまう。
    • good
    • 0
この回答へのお礼

ご回答ありがとうございました。
参考になりました。

上の方のコードで分かったんですが、そう言えば?なんかも改行に入りますね・・・。
全然思いつきませんでした。
と考えるとかなり難しい気がしてきました。

お礼日時:2012/06/03 00:01

横ですみません。



> 海外の方たちはどのようにして対処しているのかと思います。

海外の方たちは特に、「改行されていない文章をピリオド毎に改行する」
必要性を感じていないのでは…?
段落でまとまった文章の方が読みやすいですし。

そもそも、質問主さまはどのような目的で、ピリオドで改行なさろうと
されているのでしょうか?
その目的によって、プログラムの落としどころ
(何を実装して、何に目をつぶるか)も決まってくると思います。
    • good
    • 0

ピリオドの後ろにスペースが入っていてその後大文字から始まる


という条件ならこんな感じ?

<?PHP
$str="A is a. B is b. c is C. D is d? E is e! F is f.";
$pattern="/[\.\?\!](?= [A-Z])/";
$replacement="$0\n";
$str=preg_replace($pattern,$replacement,$str);
print nl2br($str);
?>
    • good
    • 0
この回答へのお礼

ご回答本当にありがとうございました。

おおっ、と思って以下の文に試してみたところ、英文と言うのは()があったり、"aaaa."のような形も取るようです。
思っていたよりかなり難しいですね。
海外の方たちはどのようにして対処しているのかと思います。


The second time proved to be the charm for space engineers in the United States. They successfully launched a rocket to send an unmanned module to the International Space Station after a failed attempt three days ago.
If all goes according to plan, this will be the first private cargo delivery to the ISS.

(George Diller / NASA Launch Commentator)
"Five, four, three, two, one, zero, and launch of the SpaceX Falcon 9 rocket as NASA turns to the private sector to resupply the International Space Station."

The rocket lifted off Tuesday from an American air force base in Florida. It's carrying a spacecraft called Dragon. Engineers at the U.S.-based Space Exploration Technologies, better known as SpaceX, created it. They'll control it, too.
The module separated from the rocket about 10 minutes after liftoff. It's now orbiting the Earth, roughly 320 kilometers above the surface.
Engineers will try later this week to get the Dragon to dock at the ISS. The operation requires extreme precision because the station is orbiting the Earth at eight kilometers per second — faster than a speeding bullet.

お礼日時:2012/06/03 00:00

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!