?
Solved

PHP and different CSS class on comas inside string of numbers

Posted on 2012-03-27
2
Medium Priority
?
354 Views
Last Modified: 2012-08-13
Hi all.

I am in need of expert help with PHP and CSS. I am trying to create a count ticker with a different css class assigned to the coma in a number string. The number string get pulled from my database.

For example, my number is 11,229,228 how could I have PHP apply different css class to each individual digit and each individual coma.

The finished code should look like this:

<div style="float:left">
<div class="number">1</div>
<div class="number">1</div>
<div class="coma">,</div>
<div class="number">2</div>
<div class="number">2</div>
<div class="number">9</div>
<div class="coma">,</div>
<div class="number">2</div>
<div class="number">2</div>
<div class="number">8</div>
</div>

Open in new window


I have tried to preg_replace(); option, but do not seem to get my head around to make it work.
0
Comment
Question by:slickip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
Tobias earned 2000 total points
ID: 37771651
Dear,

This work :

<?php

$str = '11,229,228';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);

echo '<div style="float:left">';

foreach ($chars as $char) {

	if(is_numeric($char))
	{	
	echo "<div class=\"number\">$char</div>";
	}

	if($char==",")
	{	
	echo "<div class=\"coma\">,</div>";
	}	

}

echo '</div>';

?>

Open in new window



EDIT : forgot the first div with float

Regards
0
 

Author Closing Comment

by:slickip
ID: 37771716
PERFECT! That was a very fast reply indeed!

Thank you, your help was appreciated.

Regards.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question