Php 替换echo中的字符串

Php 替换echo中的字符串,php,Php,我试图通过URL在标题之间用-替换空格,但在echo命令中很难做到这一点 字符串: <a href=\"entry/{$article['id']} {$article['news_title']}\"> {$article['news_title']} </a> 我试着做: <a href=\"entry/{$article['id']} .'-' .stripslashes(str_replace(' ', '-',

我试图通过URL在标题之间用
-
替换空格,但在
echo
命令中很难做到这一点

字符串:

<a href=\"entry/{$article['id']} {$article['news_title']}\">
    {$article['news_title']}
</a>

我试着做:

<a href=\"entry/{$article['id']}
        .'-'
        .stripslashes(str_replace(' ', '-', {$article['news_title']}))
        .'\">
    {$article['news_title']}
</a>    

但它会抛出错误。以下是完整的代码:

 echo("
        <a href=\"entry/{$article['id']} {$article['news_title']}\">
            {$article['news_title']}
        </a>
      ");   
echo(“
");   

您需要结束引号,以便调用函数并使用串联

echo "
    <a href=\"entry/{$article['id']} {" . stripslashes(str_replace(' ', '-', $article['news_title'])) . "\">{$article['news_title']}</a>        

  ";
echo”
";
但是,为了可读性,我建议使用一个变量:

$title_url = stripslashes(str_replace(' ', '-', $article['news_title']));
echo("
    <a href=\"entry/{$article['id']} {$title_url}\">{$article['news_title']}</a>        

  ");   
$title\u url=stripslashes(str\u replace(“,”,$article['news\u title'));
回声(“
");   
您是否尝试过以下方法:

<?php 
$url = "entry/".{$article['id']}.{$article['news_title']};
$encoded = rawurlencode ( $url );
echo '<a href="'.$encoded.'">'.{$article['news_title']}.'</a>';
?>