Php 如何在每个单词后加负号?

Php 如何在每个单词后加负号?,php,regex,Php,Regex,我正在youtube视频源上工作,我想将每个视频标题转换为seotitle 我有这样一个字符串:“利昂国王-使用某人” 我希望它是这样的:'Kings-Of-Leon-Use-someone' 我尝试了以下代码: $seotitle=str_replace(' ','-',$video['title']['$t']); 但我得到了这个“利昂国王使用的——某人” 处理额外空格和减号的好方法是什么?一个简单的正则表达式可以满足您的需要: preg_replace('|[ -]+|', '-', $

我正在youtube视频源上工作,我想将每个视频标题转换为seotitle

我有这样一个字符串:
“利昂国王-使用某人”

我希望它是这样的:
'Kings-Of-Leon-Use-someone'

我尝试了以下代码:

$seotitle=str_replace(' ','-',$video['title']['$t']);
但我得到了这个“
利昂国王使用的——某人”


处理额外空格和减号的好方法是什么?

一个简单的正则表达式可以满足您的需要:

preg_replace('|[ -]+|', '-', $seotitle);

一个简单的正则表达式可以执行以下操作:

preg_replace('|[ -]+|', '-', $seotitle);
试试这个:

$string = 'Kings Of Leon - Use Somebody';

// first replace anything but letters with space
$string = preg_replace('/[^a-zA-Z]/', ' ', $string);

// then replace consecutive spaces with hypen
$string = preg_replace('/[ ]+/', '-', trim($string));
有关详细信息,请参阅内联注释。

尝试以下注释:

$string = 'Kings Of Leon - Use Somebody';

// first replace anything but letters with space
$string = preg_replace('/[^a-zA-Z]/', ' ', $string);

// then replace consecutive spaces with hypen
$string = preg_replace('/[ ]+/', '-', trim($string));

有关详细信息,请参见内联注释。

您可以使用相同的函数将“-”替换为“”,如下所示:

$seotitle_temp=str_replace(' - ', ' ', 'Kings Of Leon - Use Somebody'); 
$seotitle_temp=str_replace(' ', '-', 'Kings Of Leon - Use Somebody');

然后您将清除。

您可以使用相同的函数将“-”替换为“”,如下所示:

$seotitle_temp=str_replace(' - ', ' ', 'Kings Of Leon - Use Somebody'); 
$seotitle_temp=str_replace(' ', '-', 'Kings Of Leon - Use Somebody');

然后你就可以离开了。

试试这个:

//string assign to variable
$string = 'Kings Of Leon - Use Somebody';
//Removes whitespace or other predefined characters from the right side of a string
$string = ltrim($string);
// Removes whitespace or other predefined characters from both sides of a string
$string = rtrim($string);
// str replace methods
$string = str_replace(" ", "-", $string);
// RemoveSpecial Character
$string = preg_replace("/[^A-Za-z0-9\-]/", "",$string);
// Remove Multiple -
$string = preg_replace('/-+/', '-',$string);

试试这个:

//string assign to variable
$string = 'Kings Of Leon - Use Somebody';
//Removes whitespace or other predefined characters from the right side of a string
$string = ltrim($string);
// Removes whitespace or other predefined characters from both sides of a string
$string = rtrim($string);
// str replace methods
$string = str_replace(" ", "-", $string);
// RemoveSpecial Character
$string = preg_replace("/[^A-Za-z0-9\-]/", "",$string);
// Remove Multiple -
$string = preg_replace('/-+/', '-',$string);

使用正则表达式函数,例如
preg\u replace
-您可以使用
+
设备指定字符数。查看php.net/preg_replace了解更多信息。使用正则表达式函数,例如
preg_replace
-您可以使用
+
设备指定一些字符。查看php.net/preg_replace以了解更多信息。标题中没有-这将替换此处的行“Leon国王-使用某人”,然后将空格替换为“-”,他得到“Leon国王使用某人”。试试看。我得到的标题只有空格,没有破折号。用破折号替换空格。不,它用空格替换字符串中的一个破折号<代码>str_替换('-',''Leon国王-使用某人')成为利昂国王使用某人这在标题中没有任何-这将替换这里的行“利昂国王-使用某人”,然后将空格替换为“-”,他得到“利昂国王使用某人”。试试看。我得到的标题只有空格,没有破折号。用破折号替换空格。不,它用空格替换字符串中的一个破折号<代码>str_替换('-',''Leon国王-使用某人')成为利昂国王使用某人