Log in

View Full Version : Problem with this function FormatText



titanite
06-02-2006, 01:48 PM
Problem with this function FormatText
-----------------
Hi guys,

I have a problem with this function called formatText.

It helps me churns the data input into my mysql database into proper paragraphs, takes care of the accented characters etc.. it's perfect, except for one point: I cannot add <a href></a> into my text.
Well I CAN, but it will append the page folder to whatever I put.

Example:
- When I have this text "And here we go to Google".
- I insert the text with the <a> tag into my mysql database: And here we go to <a href="http://w w w xxx>">Google</a> [note, this is only an example so I'm not putting the real address here]. My web addres is http://www.example.com/thispage.php
- on the page that is churned out, the text is welll formatted but the link turned out to be "http://www.example.com/http://w w w xxx".
- Which is to say it appends whatever URL the page is located to the link within the text.

Why is this happening?

Here is the formatText function:
_________________


<?
function formatText ($text, $addPara=1, $addBreakLine=1, $stripTags=0, $allowedTags='<a>,<em>,<strong>,<i>,<b>') {

$text = trim ($text);

# translate all accented characters to HTML entities
# but keep all HTML tags
$table = get_html_translation_table (HTML_ENTITIES);
$table['<'] = '<';
$table['>'] = '>';
$text = strtr ($text, $table);

# strip all HTML tags if the param $stripTags is set to 1
# keep the tags that are set in the param $allowedTags
if ($stripTags) {
if ($allowedTags) {
$text = strip_tags ($text, $allowedTags);
} else {
$text = strip_tags ($text);
}
}

# add <p> if the param $addPara is set to 1 (default)
if ($addPara) {
$textElements = preg_split ("/\n(\s)+/U", $text);
$newText = "";
foreach ($textElements as $item) {
#be sure to strip control chars
$item = trim ($item);
if ( strlen ($item) != 0 ) {
$newText .= "<p>$item</p>";
}
}
$text = $newText;
}

# add <br> if the param $addBreakLine is set to 1 (default)
if ($addBreakLine) {
$table = null;
$text = nl2br($text);
# nl2br will strip the 'n' after the </p>, add it here
# to have a nice formatted HTML source
$table["</p>"] = "</p>\n\n";
$text = strtr ($text, $table);
}

return $text;
}
?>

djr33
06-02-2006, 05:14 PM
$striptags is set to 0 at the start. Why would the part of the function that "if $striptags == 1" work at all?


And.... no, sorry. I don't see where the <a> is modified in the code.

I'm not familiar with some of the syntax, so someone else can probably help.

Twey
06-02-2006, 07:15 PM
The problem is here, I believe:
# translate all accented characters to HTML entities
# but keep all HTML tags
$table = get_html_translation_table (HTML_ENTITIES);
$text = strtr ($text, $table);
It's replacing some special characters in the url (for example, http%3e%2f%2fwww%2egoogle%2ecom%2f) and so causing the URL to be treated as relative.

$striptags is set to 0 at the start. Why would the part of the function that "if $striptags == 1" work at all?That's a default value. For example:
function ds($b = 3) {
return $b;
}

ds(); // returns 3
ds(5); // returns 5

djr33
06-02-2006, 09:31 PM
Oh, ok. Yeah... not used to functions. Sorry. But.. ok :)

Yeah, that reason makes sense.

What would the solution be, though?

I think you'd need to write a new function for <a> tags, right? and manually code it to look for the part after href=", and NOT change that, until it gets to a ".

Twey
06-03-2006, 11:36 AM
The best method is probably to replace at either end like so:
# translate all accented characters to HTML entities
# but keep all HTML tags
$table = get_html_translation_table (HTML_ENTITIES);
$table['<'] = '<';
$table['>'] = '>';
$table['.'] = '.'; // I'm not familiar with this table; it may or may not replace .
// Either way, it doesn't hurt to do this. You can remove it
// if it isn't necessary.
$text = str_replace("://", "[[[URL_PROTOCOL_HANDLER]]]", $text);
$text = strtr ($text, $table);
$text = str_replace("[[[URL_PROTOCOL_HANDLER]]]", "://", $text);

titanite
06-06-2006, 08:06 AM
Hi guys,

It's still not working. I still have the URL of the page appended to the front of my link: http://www.ei-ie.org/en/news/show.php?id=199&theme=hivaids&country=global

I'm sorry I'm such a php idiot.. I have replaced the bit where Professor Twey advised to replace, but still it is not working :((

Twey
06-06-2006, 12:11 PM
Aha! The mailto? Add:
$table['"'] = '"';in there somewhere.

titanite
06-06-2006, 01:37 PM
YES!!! It worked perfectly!!!!!!!!!! Thank you Professor!!! :))))))))
xoxoxoxox
HUGZZZZZZZZZZZ ;)

titanite
06-06-2006, 01:39 PM
I'm pasting the whole function in case anyone needs it, it formats your text into paragraphs and breaks, and is useful when you need to write in languages other than English, due to accented characters etc.
So here goes, thanks to the efforts of Twey and Djr33!! ;)

________________________

<?
function formatText ($text, $addPara=1, $addBreakLine=1, $stripTags=0, $allowedTags='<a>,<em>,<strong>,<i>,<b>') {

$text = trim ($text);

# translate all accented characters to HTML entities
# but keep all HTML tags
$table = get_html_translation_table (HTML_ENTITIES);
$table['<'] = '<';
$table['>'] = '>';
$table['"'] = '"';
$table['.'] = '.'; // I'm not familiar with this table; it may or may not replace .
// Either way, it doesn't hurt to do this. You can remove it
// if it isn't necessary.
$text = str_replace("://", "[[[URL_PROTOCOL_HANDLER]]]", $text);
$text = strtr ($text, $table);
$text = str_replace("[[[URL_PROTOCOL_HANDLER]]]", "://", $text);

# strip all HTML tags if the param $stripTags is set to 1
# keep the tags that are set in the param $allowedTags
if ($stripTags) {
if ($allowedTags) {
$text = strip_tags ($text, $allowedTags);
} else {
$text = strip_tags ($text);
}
}

# add <p> if the param $addPara is set to 1 (default)
if ($addPara) {
$textElements = preg_split ("/\n(\s)+/U", $text);
$newText = "";
foreach ($textElements as $item) {
#be sure to strip control chars
$item = trim ($item);
if ( strlen ($item) != 0 ) {
$newText .= "<p>$item</p>";
}
}
$text = $newText;
}

# add <br> if the param $addBreakLine is set to 1 (default)
if ($addBreakLine) {
$table = null;
$text = nl2br($text);
# nl2br will strip the 'n' after the </p>, add it here
# to have a nice formatted HTML source
$table["</p>"] = "</p>\n\n";
$text = strtr ($text, $table);
}

return $text;
}
?>

<? while ($row = mysql_fetch_array ($result)) {?>
<h4>[<? echo $row["date"];?>] <? echo $row["title"];?></h4>
<p><strong><? echo $row["teaser"];?></strong></p>
<p><? echo formatText ($row["text"]);?></p>
<? };?>