去掉一个字母单词之间的空格

发布于 2024-11-05 23:52:10 字数 227 浏览 1 评论 0原文

你如何去掉一个字母单词之间的空格。示例:

  • 字符串:“TIGRUS FOO and Drezga d . o . o . New York”
  • 我们想要:“TIGRUS FOO and Drezga doo New York”

如果可能的话 preg_replace 解决方案,抱歉因为之前没有告诉。

How would you strip spaces between one letter words. Example:

  • string: "T I G R U S FOO and Drezga d . o . o . New York"
  • we want: "TIGRUS FOO and Drezga d.o.o. New York"

If possible preg_replace solution, sorry for not telling before.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够钟 2024-11-12 23:52:10

以下作品。

$string = "T I G R U S FOO and Drezga d . o . o . New York";

$words = explode(" ", $string);

$output = array();
$temp_word = "";
foreach($words as $word)
{
    if (strlen($word) == 1)
    {
        $temp_word .= $word;
    }
    else
    {
        if ($temp_word != "")
        {
            $output[] = $temp_word;
            $temp_word = "";
        }
        $output[] = $word;
    }
}

$output = implode(" ", $output);
echo $output;

输出:“TIGRUS FOO 和 Drezga doo New York”

The following works.

$string = "T I G R U S FOO and Drezga d . o . o . New York";

$words = explode(" ", $string);

$output = array();
$temp_word = "";
foreach($words as $word)
{
    if (strlen($word) == 1)
    {
        $temp_word .= $word;
    }
    else
    {
        if ($temp_word != "")
        {
            $output[] = $temp_word;
            $temp_word = "";
        }
        $output[] = $word;
    }
}

$output = implode(" ", $output);
echo $output;

Outputs: "TIGRUS FOO and Drezga d.o.o. New York"

溇涏 2024-11-12 23:52:10

试试这个 php 代码:

<?php
   $str = "T I G R U S FOO and Drezga d . o . o . New York";
   $out = preg_replace('~(\b.)\s~', "\\1", $str);
   var_dump($out);
?>

OUTPUT

string(38) "TIGRUSFOO and Drezga d. o. o. New York

UPDATE

<?php
   $str = "T I G R U S FOO and Drezga d . o . o . New York N Y";
   $s = preg_replace('~((?<=^[^\s])|(?<=\s[^\s]))\s(?=[^\s](\s|$))~', "", $str);
   var_dump($s);
?>

OUTPUT

string(40) "TIGRUS FOO and Drezga d.o.o. New York NY"

?>

Try this php code:

<?php
   $str = "T I G R U S FOO and Drezga d . o . o . New York";
   $out = preg_replace('~(\b.)\s~', "\\1", $str);
   var_dump($out);
?>

OUTPUT

string(38) "TIGRUSFOO and Drezga d. o. o. New York

UPDATE

<?php
   $str = "T I G R U S FOO and Drezga d . o . o . New York N Y";
   $s = preg_replace('~((?<=^[^\s])|(?<=\s[^\s]))\s(?=[^\s](\s|$))~', "", $str);
   var_dump($s);
?>

OUTPUT

string(40) "TIGRUS FOO and Drezga d.o.o. New York NY"

?>

情定在深秋 2024-11-12 23:52:10

不幸的是,这是不可能的。没有简单的方法可以区分单空格单词边界和单词字符之间的无关单空格。

编辑:已撤回 - 我假设替换需要正确处理(即保持独立)单词实际上是一个字符长。

That's not possible, unfortunately. There is no easy way to distinguish single-space word boundaries from extraneous single-spaces between a word's characters.

EDIT: Retracted -- I made the assumption that the replacement would need to properly handle (i.e. keep separate) words that are actually one character long.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文