Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-拆分标记并用空格连接_Php - Fatal编程技术网

PHP-拆分标记并用空格连接

PHP-拆分标记并用空格连接,php,Php,我有一个由分隔的标记列表,我已经拆分了它们,现在我想在它们之间连接一个空格,以便它们可以在数据标记属性中使用。我该怎么做 <div data-tags="<?php foreach ($lab->tags()->split(',') as $tag): ?> <?php echo str::slug($tag)?> <?php endforeach ?>" class="lab-cnt em-below"> 理想的输出是 //

我有一个由
分隔的标记列表,
我已经拆分了它们,现在我想在它们之间连接一个空格,以便它们可以在
数据标记
属性中使用。我该怎么做

<div data-tags="<?php foreach ($lab->tags()->split(',') as $tag): ?>
   <?php echo str::slug($tag)?>
<?php endforeach ?>" class="lab-cnt em-below">
理想的输出是

// data-tags="pavillion industrial commercial"

使用串联运算符在第2行的echo语句中添加空格,空格用引号括起来
-

像这样-

<?php echo str::slug($tag)." "?>

因此,您的代码现在将变为-

<div data-tags="<?php foreach ($lab->tags()->split(',') as $tag): ?>
   <?php echo str::slug($tag)." "?>
<?php endforeach ?>" class="lab-cnt em-below">

使用串联运算符在第2行的echo语句中添加空格,空格用引号括起来
-

像这样-

<?php echo str::slug($tag)." "?>

因此,您的代码现在将变为-

<div data-tags="<?php foreach ($lab->tags()->split(',') as $tag): ?>
   <?php echo str::slug($tag)." "?>
<?php endforeach ?>" class="lab-cnt em-below">

简单的方法就是在每个echo的末尾添加一个空格字符
'

<div data-tags="<?php foreach ($lab->tags()->split(',') as $tag): ?>

     <?php echo str::slug($tag).' ' ?>

<?php endforeach ?>" class="lab-cnt em-below">

简单的方法就是在每个echo的末尾添加一个空格字符
'

<div data-tags="<?php foreach ($lab->tags()->split(',') as $tag): ?>

     <?php echo str::slug($tag).' ' ?>

<?php endforeach ?>" class="lab-cnt em-below">

使用
分解
内爆
功能:

$tags = $lab->tags();
// 'pavillion,industrial,commercial'

$tags = explode(',', 'pavillion,industrial,commercial');
// ['pavillion', 'industrial', 'commercial']

$tags = array_map(str::slug, $tags);
// slugified ['pavillion', 'industrial', 'commercial']

$tags = implode($tags, ' ');
// 'pavillion industrial commercial'

使用
分解
内爆
功能:

$tags = $lab->tags();
// 'pavillion,industrial,commercial'

$tags = explode(',', 'pavillion,industrial,commercial');
// ['pavillion', 'industrial', 'commercial']

$tags = array_map(str::slug, $tags);
// slugified ['pavillion', 'industrial', 'commercial']

$tags = implode($tags, ' ');
// 'pavillion industrial commercial'

欢迎来到StackOverflow。我已经回答了这个问题,如果它解决了您的问题,请将答案标记为正确。欢迎来到StackOverflow。我已经回答了这个问题,如果它解决了您的问题,请将答案标记为正确。添加了更多信息@Martin添加了更多信息@Martin想在我的答案中添加这一点,但选择坚持用户的代码。我的错;)在我的答案中加入了这一点,但选择了坚持用户代码。我的错;)