Hướng dẫn text formatting 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:

Nội dung chính

  • Description
  • Return Values
  • How do you format text?
  • What is a format string in PHP?
  • How do you format text in HTML?
  • Can we format text in HTML?

Calappa nucifera [L.] Kuntze

Cocos indica Royle

But my code shows:

Calappa nucifera [L.] Kuntze

Cocos indica Royle

My code is below:

echo '
'.str_replace[',','
',$row["synonyms"]].'
';

ScaisEdge

130k10 gold badges89 silver badges100 bronze badges

asked Aug 20, 2016 at 13:29

5

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 '
' ; foreach [$mySino as $key => $myRow] { $myValue= explode[ ' ', $myRow, 3]; echo '' . [isset[$myValue[0]] ? $myValue[0] : ''] . ' ' . [isset[$myValue[1]] ? $myValue[1] : '' ] . ' ' .[ isset[$myValue[2]] ? $myValue[2] : ''] . '
'; } echo '
' ;

answered Aug 20, 2016 at 13:33

ScaisEdgeScaisEdge

130k10 gold badges89 silver badges100 bronze badges

16

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


Output

Through I suggest using something like Parsedown

answered Aug 20, 2016 at 13:39

Script47Script47

13.8k4 gold badges43 silver badges61 bronze badges

4

[PHP 4, PHP 5, PHP 7, PHP 8]

sprintfReturn a formatted string

Description

sprintf[string $format, mixed ...$values]: string

Parameters

format

The format string is composed of zero or more directives: ordinary characters [excluding %] that are copied directly to the result and conversion specifications, each of which results in fetching its own parameter.

A conversion specification follows this prototype: %[argnum$][flags][width][.precision]specifier.

Argnum

An integer followed by a dollar sign $, to specify which number argument to treat in the conversion.

Flags FlagDescription
- Left-justify within the given field width; Right justification is the default
+ Prefix positive numbers with a plus sign +; Default only negative are prefixed with a negative sign.
[space] Pads the result with spaces. This is the default.
0 Only left-pads numbers with zeros. With s specifiers this can also right-pad with zeros.
'[char] Pads the result with the character [char].
Width

An integer that says how many characters [minimum] this conversion should result in.

Precision

A period . followed by an integer who's meaning depends on the specifier:

  • For e, E, f and F specifiers: this is the number of digits to be printed after the decimal point [by default, this is 6].
  • For g, G, h and H specifiers: this is the maximum number of significant digits to be printed.
  • For s specifier: it acts as a cutoff point, setting a maximum character limit to the string.

Note: If the period is specified without an explicit value for precision, 0 is assumed.

Note: Attempting to use a position specifier greater than PHP_INT_MAX will generate warnings.

Specifiers SpecifierDescription
% A literal percent character. No argument is required.
b The argument is treated as an integer and presented as a binary number.
c The argument is treated as an integer and presented as the character with that ASCII.
d The argument is treated as an integer and presented as a [signed] decimal number.
e The argument is treated as scientific notation [e.g. 1.2e+2].
E Like the e specifier but uses uppercase letter [e.g. 1.2E+2].
f The argument is treated as a float and presented as a floating-point number [locale aware].
F The argument is treated as a float and presented as a floating-point number [non-locale aware].
g

General format.

Let P equal the precision if nonzero, 6 if the precision is omitted, or 1 if the precision is zero. Then, if a conversion with style E would have an exponent of X:

If P > X ≥ −4, the conversion is with style f and precision P − [X + 1]. Otherwise, the conversion is with style e and precision P − 1.

G Like the g specifier but uses E and f.
h Like the g specifier but uses F. Available as of PHP 8.0.0.
H Like the g specifier but uses E and F. Available as of PHP 8.0.0.
o The argument is treated as an integer and presented as an octal number.
s The argument is treated and presented as a string.
u The argument is treated as an integer and presented as an unsigned decimal number.
x The argument is treated as an integer and presented as a hexadecimal number [with lowercase letters].
X The argument is treated as an integer and presented as a hexadecimal number [with uppercase letters].

Warning

The c type specifier ignores padding and width

Warning

Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results

Variables will be co-erced to a suitable type for the specifier:

Type Handling TypeSpecifiers
string s
int d, u, c, o, x, X, b
float e, E, f, F, g, G, h, H
values

Return Values

Returns a string produced according to the formatting string format.

Changelog

VersionDescription
8.0.0 This function no longer returns false on failure.

Examples

Example #1 Argument swapping

The format string supports argument numbering/swapping.

The above example will output:

There are 5 monkeys in the tree

However imagine we are creating a format string in a separate file, commonly because we would like to internationalize it and we rewrite it as:

We now have a problem. The order of the placeholders in the format string does not match the order of the arguments in the code. We would like to leave the code as is and simply indicate in the format string which arguments the placeholders refer to. We would write the format string like this instead:

An added benefit is that placeholders can be repeated without adding more arguments in the code.

When using argument swapping, the n$ position specifier must come immediately after the percent sign [%], before any other specifiers, as shown below.

Example #2 Specifying padding character

Nathan Alan

5 years ago

Just wanted to add that to get the remaining text from the string, you need to add the following as a variable in your scanf

%[ -~]

Example:

sscanf[$sql, "[%d,%d]%[ -~]", $sheet_id, $column, $remaining_sql];

dwieeb at gmail dot com

12 years ago

If you use the default padding specifier [a space] and then print it to HTML, you will notice that HTML does not display the multiple spaces correctly. This is because any sequence of white-space is treated as a single space.

To overcome this, I wrote a simple function that replaces all the spaces in the string returned by sprintf[] with the character entity reference " " to achieve non-breaking space in strings returned by sprintf[]



The above example will output:
The 15 monkeys are attacking the [       zoo]!

Anonymous

5 years ago

Be cafeful while trying to refactor longer strings with repeated placeholders like

    sprintf["Hi %s. Your name is %s", $name, $name];

to use argument numbering:

   sprintf["Hi %1$s. Your name is %1$s", $name];

This will nuke you at **runtime**, because of `$s` thing being handled as variable. If you got no $s for substitution, notice will be thrown.

The solution is to use single quotes to prevent variable substitution in string:

   sprintf['Hi %1$s. Your name is %1$s', $name];

If you need variable substitution, then you'd need to split your string to keep it in single quotes:

   sprintf["Hi " . '%1$s' . ". Your {$variable} is " . '%1$s', $name];

viktor at textalk dot com

13 years ago

A more complete and working version of mb_sprintf and mb_vsprintf. It should work with any "ASCII preserving" encoding such as UTF-8 and all the ISO-8859 charsets. It handles sign, padding, alignment, width and precision. Argument swapping is not handled.

Chủ Đề