自制的 php 函数似乎添加了不需要的空格


Homemade php function seems to add unwanted whitespace

我有一个美学问题,让我无休止地烦恼:以下函数将 mysql 表中的名称重新洗牌为

"姓氏,名字(某事("

"名字姓氏"

(例如,"列侬,约翰"变成了"约翰列侬"。

<?php
function rearrangeName($name){
    $cut = strpos($name, "("); //locate position of opening bracket if 
        //there is one
    if($cut != ""){
        $name_without_brackets = substr($name, 0, $cut-1);//throw away 
        //stuff between brackets
    }
    else{
        $name_without_brackets = $name;
    }
    $komma = strpos($name_without_brackets, ","); //see if there are commas
    if($komma == FALSE){
        $newName = $name_without_brackets;
    }
    else{
        $newName1 = explode(", ", $name_without_brackets);//split
            //$name_without_brackets and put First name before Last name
        $newName = ($newName1[1] . "&nbsp" . $newName1[0]);
    }
    print $newName;
}
?>

类似的函数仅返回姓氏。它们工作正常,但显然添加了空格,因此当我在其他函数中使用它们在括号之间与它们的词曲作者一起播放歌曲时,结果不是我想要的。而不是

(Lennon & McCartney), (John Lennon) and (Harrison, Lennon, McCartney, Starr)

我得到

( Lennon & McCartney ), ( John Lennon) and ( Harrison , Lennon , McCartney , Starr )

空白很多,除了"约翰列侬"结尾的一个。如果不是因为缺少额外的空格,我什至可能没有注意到它。

我已经向左,向右和中心寻找解决方案,但没有发现有效的解决方案。尝试在每个可能的级别进行修剪:不起作用。已尝试删除存在的每个制表符或换行符:不起作用。尝试摆弄 css:不起作用。

问题是我在链接中使用此功能吗?(并不是说我找到了这个方向的任何指示,但我想我只是提到它。

Stiliyan的要求(见下文(,这里有一些更多的背景信息。

第二个重排函数如下所示:

    <?php
function rearrangeName2($name){
    $cut = strpos($name, "("); //locate position of opening bracket if 
        //there is one
    if($cut != ""){
        $name_without_brackets = substr($name, 0, $cut-1);//throw away 
             //stuff between brackets
    }
    else{
        $name_without_brackets = $name;
    }
    $newName1 = explode(", ", $name_without_brackets);//split
       // $name_without_brackets and only keep Last Name
    $newName = $newName1[0];
    print $newName;
}
?>

这两个函数都在下一个函数中调用:

<?php
function songwriter($songId){ //for one, two or many authors
    include 'connect_to_database.php';
    $result = mysqli_query($con, "SELECT Author_ID, Artist FROM
    songs_with_authors, artists
     WHERE Author_ID = artists.ID 
     AND Song_ID =" . $songId . "
     ORDER BY Artist");
     $row = mysqli_fetch_array($result);    
     if(mysqli_num_rows($result) > 1){
        if(mysqli_num_rows($result) == 2){ ?>//
            <a href="results_by_author.php?nummer=<?php echo
            $row['Author_ID']; ?>">
            <?php rearrangeName2($row['Artist']); ?></a>
            <?php $row = mysqli_fetch_array($result);
            echo " &amp; "; ?>
            <a href="results_by_author.php?nummer=<?php echo  
            $row['Author_ID']; ?>">
            <?php rearrangeName2($row['Artist']); ?></a>
            <?php
        }
        else{ ?>
            <a href="results_by_author.php?nummer=<?php echo 
            $row['Author_ID']; ?>">
            <?php rearrangeName2($row['Artist']); ?></a>
                <?php $row = mysqli_fetch_array($result);
                while ($row){
                    echo ", "; ?>
                    <a href="results_by_author.php?nummer=<?php echo 
                    $row['Author_ID']; ?>">
                    <?php rearrangeName2($row['Artist']); ?></a>
                <?php $row = mysqli_fetch_array($result);
                }
            }
    }
    else{ ?>
        <a href="results_by_author.php?nummer=<?php echo $row['Author_ID'];  
            ?>">
            <?php rearrangeName($row['Artist']); ?></a><?php
    }
}
?>

使用的日期表如下所示:首先是"艺术家"表:

ID    Artist 
1     Lennon, John 
2     McCartney, Paul
3     Harrison, George
4     Starr, Ringo

这是"songs_with_authors"表:

ID    Song_ID    Author_ID
36    355        1
37    355        2
38    355        3
39    355        4
40    356        1
41    356        2

songs_with_authors表中的Author_ID是指艺术家表中的 ID;Song_ID是指歌曲列表表中的 ID。

词曲作者函数在一个单独的页面中调用,该页面创建一个带有歌曲标题的曲目列表,并在括号之间创建其作者,并且该页面包含在显示 LP 或 CD 详细信息的另一个页面中。但是,当我在空白页面中调用词曲作者函数时,只需要一个Song_ID作为其参数时,已经出现了不需要的空格的问题。例如词曲作者(355(给出了"哈里森,列侬,麦卡特尼,斯塔尔"。(逗号前有丑陋的空格。因为它也发生在一个完全不同的环境中,我在古典唱片上显示一个作曲家列表,其中额外的空格有时会导致斜杠出现在一行的开头,而不管 &nbsp,我认为重新排列函数一定是罪魁祸首。

问题在于 songwriter 函数中锚标记和php标记之间的换行符和其他空格。以下是带有空格的输出:

<a href="results_by_author.php?nummer=1">
        Lennon</a>
            ,                 <a href="results_by_author.php?nummer=2">
                McCartney</a>
            ,                 <a href="results_by_author.php?nummer=3">
                Harrison</a>

浏览器将多余的空格压缩为单个间隔,然后显示在输出中。

要修复它并避免将来出现空格问题,请在 html 标记之后立即打开 <?php 标记,如下所示:

if(mysqli_num_rows($result) > 1){
    if(mysqli_num_rows($result) == 2){ ?>
        <a href="results_by_author.php?nummer=<?php
            echo $row['Author_ID']; ?>"><?php // no whitespace before tag
            rearrangeName2($row['Artist']); 
        ?></a><?php
        $row = mysqli_fetch_array($result);
        echo " &amp; ";
        ?><a href="results_by_author.php?nummer=<?php
            echo $row['Author_ID']; ?>"><?php // no whitespace before tag
            rearrangeName2($row['Artist']); ?></a><?php
    } 
    else { ?>
        <a href="results_by_author.php?nummer=<?php
            echo $row['Author_ID']; ?>"><?php // no whitespace before tag
        rearrangeName2($row['Artist']); ?></a><?php
        $row = mysqli_fetch_array($result);
        while ($row) {
            echo ", "; ?>
            <a href="results_by_author.php?nummer=<?php
                echo $row['Author_ID']; ?>"><?php // no whitespace before tag
            rearrangeName2($row['Artist']); ?></a><?php
        $row = mysqli_fetch_array($result);
    }
}
print str_replace(' ', $nieuwenaam);

这是你要找的吗?

function herschikNaam($naam) {
    // If name contains any parenthesis, snip until right before
    if (strpos($naam, '(') !== false)
        $naam = substr($naam, 0, (strpos($naam, '(')-1));
    $naam = explode(',', $naam);
    // If there's a comma, reorder them.
    if (count($naam) === 2) 
        return trim($naam[1]) . ' ' . trim($naam[0]);
    // No comma, give it back in same order
    else 
        return implode(' ', $naam);
}

当提供例如"列侬,约翰","列侬和麦卡特尼"时,它似乎可以做你想要的