[SOLVED] html code from database coming through as text...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Try the following code (see also http://www.php.net/manual/en/language.types.string.php).

Code: Select all

$maxlength = "180";
$newstext = <<<EODEFIN
[FMP-Field: TEXT XHTML, Raw]
EODEFIN;
 
$words = explode(" ", $newstext);
 
foreach ($words as $key => $word) {
    if ($key < $maxlength) {
        $newsdisplay .= $word." ";
    } elseif ($key == $maxlength) {
        $newsdisplay .= $word."...";
    }
}
 
echo($newsdisplay);
And let me know if that works.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

That's a great idea.

Unfortunately it doesn't work.

I get

Code: Select all

Parse error: parse error, unexpected $
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

That's the case even if I comment out the bulk of the PHP...

Code: Select all

$maxlength = "180";

$newstext = <<<EODEFIN
[FMP-Field: TEXT XHTML, Raw]
EODEFIN; 

/*
$words = explode(" ", $newstext);
 
foreach ($words as $key => $word) {
    if ($key < $maxlength) {
        $newsdisplay .= $word." ";
    } elseif ($key == $maxlength) {
        $newsdisplay .= $word."...";
    }
}
*/
 
echo($newstext);
I'm thinking of building in a feature into a database field - it's basically a content management system - to specify a tag in the field which is where the text gets cut off.
So it's being processed before it hits PHP and I can just do normal FileMaker CDML stuff.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Damn, :cry:

Code: Select all

<?php

$newsdisplay="";

$maxlength = "180";
$newstext = <<<EODEFIN
Lorem ipsum dolor sit amet, consectetuer 100$ or $100 adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
<img src="/img/myimg" alt="not found">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
EODEFIN;

$words = explode(" ", $newstext);
 
foreach ($words as $key => $word) {
    if ($key < $maxlength) {
        $newsdisplay .= $word." ";
    } elseif ($key == $maxlength) {
        $newsdisplay .= $word."...";
    }
}
 
echo($newsdisplay);
?>
appears to work. I can only guess that the error is caused by the data from the database. Can you give us the example data ?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

By the by... using <<< has the disadvantage that $x would be taken as being the variable $x and insert the variables contents. A potential security risk.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Some sample data...

Code: Select all

<img src=&quote;http://www.dealer-world.co.uk/eclub/amdmar05/220305 proshowlogo.gif&quote; alt=&quote;&quote; border=&quote;0&quote; align=&quote;right&quote; />CUSTOM CHROME DEALER SHOW, MAINZ GERMANY MARCH 19th - 20th, 2005

A total of 64 top class custom builders from Europe and beyond entered 71 bikes in the AMD ProShow, held on Saturday March 19th and Sunday March 20th, to compete for the coveted accolade of being the official Eu
Do I need an addslashes in there somewhere?
To deal with the src=""
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Using the sample code provided earlier (which simply pretents to have replaced your tag with the actual HTML) and your sample data produces no error when I try it.

Example (OK also changed the split method, but also worked with the old method).

Code: Select all

<?php
$maxlength = "180";
$newstext = <<<EODEFIN
<img src="http://www.dealer-world.co.uk/eclub/amdmar05/220305 proshowlogo.gif" alt="" border="0" align="right" />CUSTOM CHROME DEALER SHOW, MAINZ GERMANY MARCH 19th - 20th, 2005
 
A total of 64 top class custom builders from Europe and beyond entered 71 bikes in the AMD ProShow, held on Saturday March 19th and Sunday March 20th, to compete for the coveted accolade of being the official Eu
EODEFIN;

$post_txt="";
$words = explode(" ", $newstext);
if (count($words)>$maxlength) {
  $post_txt="...";
  $words=array_splice($words,0,$maxlength);
  $newsdisplay=implode(" ",$words).$post_txt;
} else {
  $newsdisplay=$newstext;
}

echo($newsdisplay);
?>
Last edited by CoderGoblin on Thu Mar 24, 2005 8:14 am, edited 1 time in total.
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Post by batfastad »

Just to let you know I've solved the problem without using PHP, I've done it in FileMaker.
I would have liked to be able to manipulate cut the data through PHP though, as it's easier to edit the PHP code than it is to take the filemaker web server offline, edit the field definition, every time I want to change the number of words that it cuts the text at.

I'll change the subject to solved but I'll keep looking at ways to do this.

Thanks for all your help

Ben
Post Reply