<p>I guess you could make use of some php functions to do this:</p>

<p><code>$string = 'Hello there';</code></p>

<p>First <a href="http://php.net/manual/en/function.explode.php" rel="nofollow">explode</a> the string using a space as the seperator</p>

<p><code>$result = explode(" ", $string);</code></p>

<p>This will give you an array like <code>array(0 =&gt; 'Hello' 1 =&gt; 'there')</code></p>

<p>Then you can <a href="http://php.net/manual/en/function.implode.php" rel="nofollow">implode</a> the array, using an <code>-</code> as the "glue" </p>

<p><code>$result = implode('-', $result);</code></p>

<p>Which will then give you <code>Hello-there</code></p>

<p>Then use <a href="http://php.net/manual/en/function.strtolower.php" rel="nofollow">strtolower</a> to make it lowercase</p>

<p><code>$result = strtolower($result);</code></p>

<p>To simplify this, you can just do it all at once</p>

<p><code>$result = strtolower(implode('-',explode(" ", 'Hello there')));</code></p>

CC BY-SA 3.0
2016-01-29T10:33:22.880Z
2016-01-29T10:33:22.880Z
Alec Ritson
<p>You can actually use the <a href="https://craftcms.com/classreference/helpers/StringHelper#toKebabCase-detail" rel="nofollow">StringHelper::toKebabCase</a>  function for this:</p>

<pre class="lang-php prettyprint-override"><code>$string = 'Hello there';
$result = StringHelper::toKebabCase($string);
// =&gt; 'hello-there'
</code></pre>

CC BY-SA 3.0
2016-01-29T11:10:42.017Z
Can you give an example on how to call this function please?
Adam Menczykowski
CC BY-SA 3.0
2016-01-29T11:14:57.913Z
Trying this: $urlslug = toKebabCase($member->membersCompanyName); results in: Call to undefined function Craft\toKebabCase()
Adam Menczykowski
CC BY-SA 3.0
2016-01-29T13:00:31.110Z
@AdamMenczykowski Just updated the answer with an example.
Brandon Kelly
CC BY-SA 3.0
2016-01-29T13:00:57.880Z
$string = StringHelper::toKebabCase('test this string'); does it!
Adam Menczykowski
CC BY-SA 3.0
2016-01-29T10:38:19.327Z
2016-01-29T13:00:03.723Z
2016-01-29T13:00:03.723Z
chris
<p>I want to convert a String into kebab-case.</p>

<pre><code>'Hello there' -&gt; 'hello-there'
</code></pre>

<p>I'm working in the Element API, so haven't got the pleasure of using the simple Twig filter. I guess there is a way of doing it in PHP. Any suggestions? Thank you</p>

<filter><plugin-elementapi>
Element API - 'String' to 'kebab-case'
# Element API - 'String' to 'kebab-case'

(Asked by: username_0 on 2016-01-29)

I want to convert a String into kebab-case.
'Hello there' -> 'hello-there'

I'm working in the Element API, so haven't got the pleasure of using the simple Twig filter. I guess there is a way of doing it in PHP. Any suggestions? Thank you

**Tags:** filter, plugin-elementapi

---

## 2 answers

### Answer by username_2 on 2016-01-29. Score: 7 | Accepted answer

You can actually use the StringHelper::toKebabCase  function for this:
$string = 'Hello there';
$result = StringHelper::toKebabCase($string);
// => 'hello-there'

> **Comments:**
> **username_0**: Can you give an example on how to call this function please?  
> **username_0**: Trying this: $urlslug = toKebabCase($member->membersCompanyName); results in: Call to undefined function Craft\toKebabCase()  
> **username_3**: @AdamMenczykowski Just updated the answer with an example.  
> **username_0**: $string = StringHelper::toKebabCase('test this string'); does it!  

---

### Answer by username_1 on 2016-01-29. Score: 1

I guess you could make use of some php functions to do this:

`$string = 'Hello there';`

First [explode](http://php.net/manual/en/function.explode.php) the string using a space as the seperator

`$result = explode(" ", $string);`

This will give you an array like `array(0 => 'Hello' 1 => 'there')`

Then you can [implode](http://php.net/manual/en/function.implode.php) the array, using an `-` as the "glue"

`$result = implode('-', $result);`

Which will then give you `Hello-there`

Then use [strtolower](http://php.net/manual/en/function.strtolower.php) to make it lowercase

`$result = strtolower($result);`

To simplify this, you can just do it all at once

`$result = strtolower(implode('-',explode(" ", 'Hello there')));`


---

