/    Sign up×
Community /Pin to ProfileBookmark

HTML in database

I’ve raw HTML stored in my db. It’s in paragraph format <p>Blaa Blaa</p> etc.

it’s safe as it’s saved from an admin page but how can I echo the first 30 or 50 works only?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@kiwisauthorNov 17.2021 — substr is the obvious answer but If I cut it at 50, 100,150 and the html tag say <p> isn't closed it's screws my website.
Copy linkTweet thisAlerts:
@kiwisauthorNov 17.2021 — strip_tags worked
Copy linkTweet thisAlerts:
@NogDogNov 17.2021 — Just because I had a few minutes while waiting for a DB query to complete...I came up with this as an idea. :)
[code=php]
function first($text, $words) {
$text = strip_tags($text);
$split = preg_split('/s+/', $text);
$words = array_slice($split, 0, (int) $words);
return implode(' ', $words);
}
$test = "<h1>Hello, World!</h1>n<p>This is a test. It is <em>only</em> a test.</p>n<p>The end.</p>";
echo first($test, 10);

// OUTPUT:
// Hello, World! This is a test. It is only a
[/code]
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...