替换 echo 中的字符串


replacing strings inside echo

我正在尝试通过我的 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 "
    <a href='"entry/{$article['id']} {" . stripslashes(str_replace(' ', '-', $article['news_title'])) . "'">{$article['news_title']}</a>        
  ";

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

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

你试过这个吗:

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