简体   繁体   中英

How to format a paragraph, with data coming from a database?

I have four lines of data coming from database. But the display section needs to implement the style in a specific paragraph.

For example, if this is the data coming from the database:

We've found that hosting a podcast while running a business is more useful than you might think — because it allows you to introspect on your own thoughts and decisions and have a history explaining why you made those bad decisions at the time. Even if you only have 10 listeners, and one of them is your dad.

The display section should show:

We've found that hosting a podcast while running a business is more useful than you might think — because it allows you to introspect on your own thoughts and decisions and have a history explaining why you made those bad decisions at the time.

Please note: Even if you only have 10 listeners, and one of them is your dad .

How to change the style for last line in the paragraph? How to insert html code in the last line?

Thank you

Couldn't you store the whole paragraph as HTML formatted text in the DB? Whoever is responsible for creating the text would have to HTML format it.

What you put in really dictates what you get back out again. If you want paragraph marks and other HTML markup, you need to either ensure that those are included when the data is written to the database, or have a reliable method of putting formatting back in.

You might be able to insert HTML markup based on certain rules using JavaScript or server-side code, although I can't think of a reliable, consistent set of rules that might apply.

In short, I think you need to rethink your data requirements. You're making life particularly hard for yourself right now.

I you only wish to remove the last sentence and you are using php:

$regex = 'We\'ve found that hosting a podcast while running a business is
more useful than you might think — because it allows you to introspect on your 
own thoughts and decisions and have a history explaining why you made those 
bad decisions at the time. Even if you only have 10 listeners, and one of 
them is your dad.';

preg_match_all('/\./',$regex,$match);

$count = count($match[0]);

if($count > 1)

{

    $str =  explode('.',$regex);

    $string = '';

    for($i = 0; $i < $count-1; $i++){

        $string .= $str[$i] . '.';

    }

    $change = str_replace ($string, '', $regex);

    echo $string . ' whatever ' . $change . ' whatever ';

} else {

echo 'There is only one sentence.';

}

But as Phil.Wheeler stated this is not very reliable, ie if your last sentence finishes in '...' or has a single '.' in it, apart from the final "full stop", this will not work.

The same is possible with javascript, jquery or others

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM