-3
[$number=$row['plate_number'];//it will have many numbers ex:78666,78686,78006

ex:$number="78666";  // random sample

in this ex:first 786 digit in red color and remaining 66 in normal color(78666)

ex1:$number="78006";  // random sample

in this ex1: we to color 786 to red, 78 digits red and followed by 00 normal color and 6 should be red(78006)

Here a link to what I want: enter image description here

YounesM
  • 2,111
  • 11
  • 27
Shakif
  • 1
  • 4
  • 1
    What have your tried so far? Please post your code. – Oliver Mar 15 '17 at 13:31
  • To go with what @Oliver said, what is your required output (Excel, HTML, etc)? What are all the rules associated with coloring of output? – mituw16 Mar 15 '17 at 13:32
  • In your question you want 786 in red and the remaining 66 in normal color. Yet in the picture, the number 78666 is completely red. – Loko Mar 15 '17 at 13:33
  • $find=array("786"); $replace=array("786"); $string1=str_replace($find,$replace,$row['plate_number']); i have tried this here only it will color only 786.... but i need other occurence as well which in image – Shakif Mar 15 '17 at 13:34
  • tht was little mistake in image – Shakif Mar 15 '17 at 13:35
  • @Shakif Again, what *rules*. Is it just the first 6, you want digits > 0, why make 1 number red and **not** the other one? – Xorifelse Mar 15 '17 at 13:35
  • it should follow from 786...... in some case numbers will be 78006...here i wanna put red color to 786...... in some case number will be 78600 here i wanna put red color to 786 remaining normal color – Shakif Mar 15 '17 at 13:38
  • more possibilities will come ..... we should follow first 7 followed by 8 followed by 6 should be red (some case in between any other digits will come that we have to put as normal color) – Shakif Mar 15 '17 at 13:40
  • So you want to match the first 3 characters of the string to be `786`, turn that to red and not doing anything with the other ones? – Xorifelse Mar 15 '17 at 13:44
  • @Xorifelse No he wants `78006` to have 7,8 and 6 to be red too. He wants those numbers red if they are in the specific order 1=7, 2=8 ,3=6 and only the first oocurences of those number apparently. – Loko Mar 15 '17 at 13:45
  • yeah, first 3 but some times 78006 will come in this case we have to put red color to 786 in between 00 will normal color – Shakif Mar 15 '17 at 13:45
  • yes your right bro @Xorifelse – Shakif Mar 15 '17 at 13:46
  • any suggestions – Shakif Mar 15 '17 at 13:47
  • @Shakif What should be the outcome when the number is: `87686` ? – Loko Mar 15 '17 at 13:48
  • 87686 in this 2nd digit 7 and last two digits 86 should red color – Shakif Mar 15 '17 at 13:50

1 Answers1

0

The best way to achieve that is to use a regex, as such :

$string = preg_replace('#([0-9]*?)(7)([0-9]*?)(8)([0-9]*?)(6)([0-9]*?)#', '$1<span style="color: red;">$2</span>$3<span style="color: red;">$4</span>$5<span style="color: red;">$6</span>$7', $string);

This regex will match :

  1. ([0-9]*?) : zero or more numeric characters, not replacing them
  2. (7) : the first occurence of 7, replacing it with <span style="color: red;">7</span>
  3. ([0-9]*?) : zero or more numeric characters, not replacing them
  4. (8) : the first occurence of 8, replacing it with <span style="color: red;">8</span>
  5. ([0-9]*?) : zero or more numeric characters, not replacing them
  6. (6) : the first occurence of 6, replacing it with <span style="color: red;">6</span>
  7. ([0-9]*?) : zero or more numeric characters, not replacing them

If 7, 8 and 6 are not found in the string in that order, this will do nothing.

EDIT : Added ? for each [0-9]* in order to make the quantifier ungreedy (as explained here)

Community
  • 1
  • 1
roberto06
  • 3,817
  • 1
  • 17
  • 29