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

Time-based image rotation script
I'm trying to write a PHP script that rotates an image based on what time of day it is. I want the

Posting Serialized Data Not Working
I am trying to send an object from one PHP file to another using POST. I serialize the object and th

Escape Latin Characters
I need to escape latin characters in an xml doc. Example: "é" is escaped to "é". I thoug

Insert Failing.
Hey,
I am making a Sign up page for a website, but the insert query into the Database does not se

Share admin accross websites
I'm using .net memberships and roles in one of my sites, but I need to be able to share logins and p

Do something every fifth time?
I'm trying to write a loop, but I want it to do something different after every fifth instance. Like

Problem in OnClientClick and OnClick sync
Hi,Strange and irritating problem: I have below button declaration in markup of the aspx page:

Pagination
Hi All,

I think I'm finally getting somewhere with pagination!

I can now submit a quer

PHP template help
Ok, I don't know if anyone can help me but I am using PHP templates to make a site. I am a newbie an

getting a website's source code as a variable? How?
Hey guys, is there any way for me to get the source code of a website as a variable for me to work w

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