Php 如何用破折号替换空白

Php 如何用破折号替换空白,php,Php,我的想法是删除特殊字符和html代码,并将空格替换为破折号 让我们一步一步地做 $text = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>"; 现在的输出将是这样的Hello world it is me and love you 那么,有没有什么方法可以修改此代码,使文本输出变得准确,正如我需要的那样Hello world it me and love you ~谢谢你你可以加上 $textout

我的想法是删除特殊字符和html代码,并将空格替换为破折号 让我们一步一步地做

$text = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>";
现在的输出将是这样的
Hello world it is me and love you
那么,有没有什么方法可以修改此代码,使文本输出变得准确,正如我需要的那样
Hello world it me and love you

~谢谢你

你可以加上

$textout = str_replace(' ', '-', $textout);

在最后一行之后,使用hypens替换空格。

您最好使用
strip\u标记来删除html标记,然后使用regexp删除所有非字母数字(或非空格)字符。然后,您只需使用
str\u replace
将空格转换为连字符即可。注意,我还添加了一行,将多个空间折叠为单个空间,因为这就是您在示例中所做的。否则,您将得到
世界--它是我
,而不是
世界--它是我

<?php    
    $text = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>";
    $text = strip_tags($text);
    $text = preg_replace('/[^a-zA-Z0-9 ]/', '', $text);

    //this is if you want to collapse multiple spaces to one
    $text = str_replace ('  ', ' ', $text); 

    $text = str_replace (' ', '-', $text);
?>

<?php    
    $text = "Hello world)<b> (*&^%$#@! it's me: and; love you.<p>";
    $text = strip_tags($text);
    $text = preg_replace('/[^a-zA-Z0-9 ]/', '', $text);

    //this is if you want to collapse multiple spaces to one
    $text = str_replace ('  ', ' ', $text); 

    $text = str_replace (' ', '-', $text);
?>