Ignore html and bbcode?


Posted on 16th Feb 2014 07:03 pm by admin

I've created a function to limit the amount of text according to the amount of words.

However it ruins html and bbcode tags.. . I only want it to have affect on everything else within $text but not bbcode & html.

Heres the code:
<?php

function containText($text, $length) {

$words= explode(' ', $text); // string to array
foreach ($words as $word) {
$break = 0;
for ($i = 0; $i < strlen($word); $i++) {
if ($break >= $length) {
$word= wordwrap($word, $length, '-
', true); //add
every $length chars
$break = 0;
}
$break++;

}
$newText[] = $word; //add word to array
}
$text = implode(' ', $newText); //array to string

$text = wordwrap($text, $length, "<br />n");
return $text;
}


$text = "PHPfreaks is awesome!!!!!
Lets Party! [img]http://awesome.gif[/img]
<img src="image.gif"> Yay!
<a href="http://www.phpfreaks.com">Woot</a>
[font=2]BIG![/font]";


print containText($text, 10);

?>

No comments posted yet

Your Answer:

Login to answer
277 Like 35 Dislike
Previous forums Next forums
Other forums

FPDF Help
I've been playing with the FPDF module. I was building a form and rolling right along and then sudde

Coefficient of a Restitution Hints
Hello. I was wondering if anyone can give me hints on how to write this program...

The coeffi

Display last record first.
I need some help how put the last record first and first record last.

Thanks

Code: &am

Oracle Connectivity
Hi Every One,

Can we access SAP from oracle database.If it possible then please spec

Baffled by Undefined Index in Simple Array: Please Help!
Hello. I have a form which posts an array to this script. However, I can't seem to access the values

Custom list order
Hi there,

I have checked this tutorial and it's great till the point where I want to display

how to get different value in second view of webdynpro?
hello guys,

In ABAP webdynpro I am trying to create one application having two views.

Track downloads' status
Hello,

I need to make somehow, some system, to track whether downloads are completed or faile

Cannot Connect to Database
I am writing an application to do annual reviews. I cannot get my script to work. Whenever it runs I

Force download script not handling files with spaces properly
I have a regular old php force download script, uses this code:

Code: header("Cache-Cont

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash