PHP中转换大小写和条带空间的最短方法
我试图在我的具体主题中添加类名。用破折号替换空格并将其转换为小写的优雅方式是什么 我已经试着放下箱子,但我还需要用破折号(-)替换空格 我的代码是这样的:PHP中转换大小写和条带空间的最短方法,php,concrete5,Php,Concrete5,我试图在我的具体主题中添加类名。用破折号替换空格并将其转换为小写的优雅方式是什么 我已经试着放下箱子,但我还需要用破折号(-)替换空格 我的代码是这样的: <body class="<?php echo strtolower($c->getCollectionName()); echo ' '; echo strtolower($c->getCollectionTypeName()); ?>"> 非常容易做到: 使用$replaced=str_replace
<body class="<?php echo strtolower($c->getCollectionName()); echo ' '; echo strtolower($c->getCollectionTypeName()); ?>">
非常容易做到:
使用$replaced=str_replace(“,“-”,$yourstring)代码>。替换将使空间转换为破折号
非常容易做到:
使用$replaced=str_replace(“,“-”,$yourstring)代码>。替换将使空间转换为破折号
用于从字符串中删除空格
用于用另一个字符替换空格。用于从字符串中删除空格
strtolower(preg_replace('/\s+/','-',trim($var)));
用于将空格替换为另一个字符。我将使用preg\u replace:
strtolower(preg_replace('/\s+/','-',trim($var)));
strtolower(preg_replace('_ +_', '-', $c->getCollectionName())
我会选择preg_替换:
strtolower(preg_replace('_ +_', '-', $c->getCollectionName())
你可以使用这个功能。。。它可以处理无限的参数
功能
<?php
function prepare() {
$arg = func_get_args ();
$new = array ();
foreach ( $arg as $value ) {
$new [] = strtolower ( str_replace ( array (
" "
), "-", $value ) );
}
return implode ( " ", $new );
}
?>
用法
<body class="<?php echo prepare($c->getCollectionName(),$c->getCollectionTypeName()); ?>">
<body class="<?php echo prepare("ABC CLASS","DEF","MORE CLASSES") ?>">
<body class="abc-class def more-classes">
您可以使用此功能。。。它可以处理无限的参数
功能
<?php
function prepare() {
$arg = func_get_args ();
$new = array ();
foreach ( $arg as $value ) {
$new [] = strtolower ( str_replace ( array (
" "
), "-", $value ) );
}
return implode ( " ", $new );
}
?>
用法
<body class="<?php echo prepare($c->getCollectionName(),$c->getCollectionTypeName()); ?>">
<body class="<?php echo prepare("ABC CLASS","DEF","MORE CLASSES") ?>">
<body class="abc-class def more-classes">
使用正则表达式,并将这些空格和特殊字符替换为下划线而不是破折号
<?php
$name = ' name word _ word - test ! php3# ';
$class_name = class_name( $name );
var_dump( $class_name );
function class_name( $name ){
return strtolower( trim( preg_replace('@[ !#\-\@]+@i','_', trim( $name ) ) , '_' ) );
}
?>
使用正则表达式,并将这些空格和特殊字符替换为下划线而不是破折号
<?php
$name = ' name word _ word - test ! php3# ';
$class_name = class_name( $name );
var_dump( $class_name );
function class_name( $name ){
return strtolower( trim( preg_replace('@[ !#\-\@]+@i','_', trim( $name ) ) , '_' ) );
}
?>
我更喜欢使用正则表达式,因为如果一个字符串有多个空格(如etc[space][space]etc
),它会将其视为一个空格。我更喜欢使用正则表达式,因为如果一个字符串有多个空格(如etc[space][space]etc
),它会将其视为一个空格。