Solved

PHP and different CSS class on comas inside string of numbers

Posted on 2012-03-27
2
352 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:
MadShiva earned 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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