Link to home
Start Free TrialLog in
Avatar of CFIL
CFILFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Formatting Text

Hello Experts ,

i have a text format in the DB i want to query the text then i want to do some Formating in the text ,

when i want to do this with my little experience is very hard ..

i try to do like this :
  str_replace

but its very hard for me for formating large text .. my text its change its not one ,,
can i choose my be from first line to 10 lines but it in the table or change the color
Do you have any idea forwhat is the best thing to formating text ..
my text is :
============================
Changes to GPs' opening hours will come into force from April, and could mean an average sized practice opening an extra three hours per week.
1             2              3
3              5             4
8          4                                        10
Ministers want more surgeries open at weekends and in the evenings, but the BMA had previously warned daytime services could be harmed by the changes.
====================
the above example is the just example i put some text together the important things is that the numbers 1       2      3 how could i formatting it by put each number in 1 line and do some change colors ..
thanx in advance
CFIL
Avatar of dr_dedo
dr_dedo
Flag of Egypt image

you should provide some pattern hee, as will there always bee a line, 3 lines of numbers each containing 3 digits separated by tabs or multiple spaces ?
a pattern is something you need to define before trying to code what you need
Avatar of CFIL

ASKER

hi ,
i don't have exact text , because i got lots of text files ,, maybe all files same as this
1             2              3
3              5             4
8          4                                        10

is there any way may be i do like that in the line 3 , the first entry which is here "1" put the color blue
the second one in the same line which is "2" put the color green . like this ..

there is space between the numbers .

thank you for your reply.
CFIL
An easy but usefull code for this.

Put/get your text into a variable, then:

$yourtext = str_replace('1', '<font color=\"#FF0000\">1</font>', $yourtext);
$yourtext = str_replace('3', '<font color=\"#545454\">2</font>', $yourtext);
etc...
Avatar of CFIL

ASKER

Chorch  , yeah i could do that and i said i can used
str_replace

but if the text is random ? maybe the first textis 1 the second text is 5 ? but the total lines and space are the same .
in this case i guess i must used somethings with lines , maybe in line 4 do that ,,, but i don't know how ?

CFIL
Avatar of CFIL

ASKER

any idea Experts !!
ASKER CERTIFIED SOLUTION
Avatar of dr_dedo
dr_dedo
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CFIL

ASKER

Perfect ,, it work grate like what i want ,,

i it like the code below it work fine ,, but how can choose which line i want ?
i have seen its start from the numbers even if i change the number 1 to 5 in the start .. is there any way to choose in which line or do you have any reference could read and understand deeply about the function you are used ..

thank you ..
<?
$data = <<<DATA
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
 
1             2              3
3              5             4
8          4                                        10 
8          4                                        10
8          4                                        10   
 
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
DATA;
$x = preg_replace('/(\d+)\s+(\d+)\s+(\d+)/','
<br><font color="#FF00FF">$1</font>
<font color="#00FF00">$2</font>
<font color="#0000FF">$3</font>
',$data);
echo $x;
?>

Open in new window

Avatar of CFIL

ASKER

Perfect
well, when i asked you earlier about patterns to follow, i wanted to know what to code.
now, you want to skip certain lines ?? is that what u need ? please proved a and example
Avatar of CFIL

ASKER

Hi ,
i mean some time i have a text which is has number in the line then text then number like
$data = <<<DATA
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
 
1             2              3
3              5             4
8          4                                        10
8          4                                        10
8          4                                        10  
 
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
i don't have exact text , because 1 got lots of text files ,, maybe all files same as this
8          4                                        10
8          4                                        10  

DATA;

your previous code was what i want , but now i want to do some develop of it so i can used the function with other files if i understand the idea if how can i choose certain number line of the text comes between it ..

thank you ,  and sorry for the lots of question ..
CFIL
it would be very difficult to allow only some lines and ignore others
you may break up your data into line
$newData = explode ("\n",$data) and specify which lines to use, but other ways are next to impossible
Avatar of CFIL

ASKER

hi ,.,

good idea about that $newData = explode ("\n",$data)
but how can i specify the line ? maybe i would like choose the line number 10 to 13 .. after do that
$newData = explode ("\n",$data)

try this


$dataArr = explode ("\n",$data);
$startLine = 10;
$endLine=13;
for ($i=$startLine-1; $i<$endLine ; $i++){
$dataArr[$i] = preg_replace('/(\d+)\s+(\d+)\s+(\d+)/','
<br><span class="style1_1">$1</span>
<span class="style1_2">$2</span>
<span class="style1_3">$3</span>
',$dataArr[$i]);
}
$data = implode  ( "\n"  , $dataArr  );
echo $data

Open in new window

Avatar of CFIL

ASKER

nice ,, i don't know what can i say ,, perfect it works fine ..

thank you very much sir for helping me ..

CFIL
glad i could help you :)