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.

Did you know?Explore Trending and Topic pages for more stories like this.
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

parse error
Parse error: syntax error, unexpected '[', expecting ')' in /Users/admin/Sites/phptest/array.php on

Windows Authentication
I have a website which is windows authenticated.
now i want something more on this application.<

Set Categories
I am creating a submission form for somebody to come in and submit products. These products are sep

Combining Two Queries
I have two scripts that each work fine by themselves. One is a search script with a variable that de

Remove letter from numeric textbox
I have a textbox that will search the employee database by entering in the employee ID and it will r

SAP Business Suite
Hi all:

Within the SAP Business Suite solution I would like to ask you the main differenc

SAP Project Module
I have seen in the SAP Project Module,plan cost and budget are not equal and even difference is sign

Checking if field is empty AND using !is_numeric
Hi lads,

Need to be able to display an error message (using isset?) that says "Cant lea

Stumped by Third Day of Month problem
I am trying to build an algorithm that will essentially do this:
Code: [Select]$this_month = Oct

preg_replace question
Hello!!

I hope someone can help me with preg_replace.

I load two tables from an extern

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