3

How to convert a string with class into a selector even if it contains many spaces between classes?

Input data:

$html_classes = 'class1    class2  class3 ';

Necessary result:

.class1.class2.class3

This example is not appropriate as there may be many spaces between classes

$result = '.' . str_replace( ' ', '.', $html_classes )
2
  • 2
    Might consider using Regular Expressions or explode() Commented Jan 18, 2019 at 22:36
  • 1
    Possible duplicate of php Replacing multiple spaces with a single space. Don't forget to use trim() as well to remove any starting or ending space. Commented Jan 18, 2019 at 22:40

5 Answers 5

2

Just replace all extra spaces to singles first. And run trim() to remove spaces on the beginning and at the end.

$html_classes = 'class1    class2  class3 ';
$html_classes = trim(preg_replace('/\s+/',' ',$html_classes));
$result = '.' . str_replace(' ','.',$html_classes);
Sign up to request clarification or add additional context in comments.

4 Comments

Why not replace directly to .?
Actually, yes! I'm just tried add extra line to show what is missed in logic.
Makes sense. I was just wondering if there was a reason not do do it in one step that I wasn't thinking of
May be only in case we can have space (or even few spaces) in the beginning or end of string — then we have to replace all extra spaces to singles first, run trim() on this result and after that replace spaces to dots to avoid extra dots on the beginning result string or unnecessary dots at the end.
1

Try this:

<?php
$html_classes = 'class1    class2  class3 ';
$parts = explode(" ", $html_classes);
$results = "";
foreach($parts as $c){
    if($c != ""){
        $results .= "." . $c;
    }
}
echo $results;
?>

The results I got:

.class1.class2.class3

Hope that helps.

1 Comment

@Christoph 's oneliner further down should be correct answer
1

My suggestion:

<?php
$html_classes = 'class1    class2  class3 ';
$result = '.' . preg_replace('/\s+/', '.', trim($html_classes));
echo $result;
?>

Regular expressions:

  • \s is a whitespace character.

  • + means one or more occurrences.

PHP (from http://php.net):

2 Comments

what if there is a space in the begining if the string?
Function trim() gets rid of spaces at both ends of the string.
1

You can do it without any extra trimming or concatenation. Find non-space characters surrounded by zero or more spaces and replace those matches with the non-space portion of the match preceded with a dot.

$html_classes = preg_replace('/\s*(\S+)\s*/', '.$1', $html_classes);

1 Comment

this is my new favvo
0

The answer is as simple as a single regex based replacement call:

<?php
$input = 'class1    class2  class3  ';
$output = preg_replace('/\s*([\w\d]+)\s*/', '.${1}', $input);
print_r($output);

The output obviously is:

.class1.class2.class3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.