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

Include ("Absolute Path") of a file?
I'm not sure what I'm doing wrong. I am writing my php code in the same directory of the file that

Mail form doesn't send Russian/Cyrillic characters correctly?
When someone enters foreign characters (like Cyrillic text, and Japanese/Chinese probably gives the

Pipe email to PHP - get mail adress from MySQL - send?
Hi all,

this is the challenge:

1) Our faculty at the college where I'm employed includ

Open browser page depending on XML results
Hello,

I have some PHP code that sends webbased from data to an external url ( in the form of

Better Method of a Member Cloud
I assume most people know how tag clouds work, where the tags most used are bigger and the least use

Consuming third party Payment Gateway API from Procedure.
Hi All,

First of all i would like to thank all the people of the oracle forum for providi

Update not working ... please help
Hi,

I am trying to update a row using an edit form by passing id of the row .. but it is not

a dificult string search
Hi I don't know a way around this. I want the user to input a password, but to make it a bit complca

Getting PHP to ammended the selected in option field
Hey Guys

thanks for everyones help so far. This site and its users are oozing awesomness.

BI in Upstream Production operations
Appreciate if you can assist in the following areas:
1) Examples of life before and after BI i

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