Akib67
Akib67

Reputation: 51

How to do text formatting in php

I want to do text formatting italic of my data which is echo from database. In my database, synonyms field their are more than one name and I retrieve all in one echo. For example Calappa nucifera (L.) Kuntze , Cocos indica Royle are store in my db. For this rezone i replace ',' with 'new line'and then echo. I want to show:

Calappa nucifera (L.) Kuntze

Cocos indica Royle

But my code shows:

Calappa nucifera (L.) Kuntze

Cocos indica Royle

My code is below:

echo '<div style = "margin-left: 150px;">'.str_replace(',','<br />',$row["synonyms"]).'</div>'; 

Upvotes: 0

Views: 8472

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133360

If you are using inline style in your code you have bold only and not italic

then obtain the firts two word and in a proper span tag ad font-style:italic;

$mySino = explode( ','),  $row["synonyms"]);

echo '<div style = "margin-left: 150px;">' ;
foreach ($mySino as $key => $myRow) {  

  $myValue= explode( ' ', $myRow,  3);

    echo  '<span style="font-style: italic;">' . 
           (isset($myValue[0]) ? $myValue[0] : '') .
       ' ' . (isset($myValue[1]) ? $myValue[1] : '' ) .
      '</span> ' .( isset($myValue[2]) ? $myValue[2] : '') . '<br />';
}

echo     '</div>'  ;

Upvotes: 1

Script47
Script47

Reputation: 14530

I'm not sure if you asking about some sort BBCode handler,

<?php
function parse($value) {
    $findAndReplace = [
        '[br]' => '<br>',
        '[i]' => '<i>',
        '[/i]' => '</i>',
        '[b]' => '<strong>',
        '[/b]' => '</strong>'
    ];

    return str_replace(array_keys($findAndReplace), array_values($findAndReplace), $value);
}

$str = '[b][i]Calappa nucifera[/i] (L.) Kuntze[br][br][i]Cocos indica[/i] Royle[/b]';

echo parse($str);

?>

Output

Through I suggest using something like Parsedown

Upvotes: 0

Related Questions