Advertisement

Create Array from String with explode()

By using the PHP explode() function you can easily convert any string to an array, provided there’s a delimiter. What’s a delimiter? It’s just the part of the string used to break it up, like a comma for example.

Example

// Create initial string of CSS classes
$css_classes = "btn, btn--large, btn--wide";

// Convert string to array
$css_classes = explode(', ', $css_classes);
<!-- Function in use -->
<a class="<?php echo $css_classes; ?>" href="#">Button!</a>

<!-- Output -->
<a class="btn btn--large btn--wide" href="#">Button!</a>

References

You might also likeCreate String from Array with implode().

Advertisement

Web designer and developer from Southampton, UK.


Comments are closed for this post.

Advertisement

Inspirational Newsletter

Join the newsletter to get the best articles, tutorials and exclusive freebies every two weeks.

No spam. You can unsubscribe at any time.