开发者

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

开发者 https://www.devze.com 2023-03-10 08:45 出处:网络
I have four lines of data coming from database. But the display section needs to implement the style in a specific paragraph.

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 r开发者_如何学JAVAunning 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, i.e. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消