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 )
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With