Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
misterpat
Hangin' Around



Joined: Mar 02, 2009
Posts: 48

PostPosted: Sun Apr 12, 2009 8:20 pm Reply with quote

I'm working on a custom script for my content module. I'm trying to grab the first, say 30 words, of my content articles to use as the content article's meta description.

I have it working so far, but its also grabbing the html tags. Is there a way to strip the html tags from what it gets from the database?

I tried the php strip_tags(), but cant figure out where to put it in the file to make it work.

Any ideas?

Here is the code that is working somewhat so far.

Code:
<?php


/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi                                */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

if (stristr(htmlentities($_SERVER['PHP_SELF']), 'meta.php')) {
    Header('Location: ../index.php');
    die();
}

// Get functions
global $commercial_license, $sitename, $slogan, $row, $db;
        $id = $_GET['pid'];
        $sql = 'SELECT text FROM nuke_pages WHERE pid=\'' . intval($id) . '\'';
        $result = $db->sql_query($sql);
        $row = $db->sql_fetchrow($result);


##################################################
# Include for Meta Tags generation               #
##################################################
$metastring = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="._CHARSET."\">\n";

$metastring .= "<META HTTP-EQUIV=\"EXPIRES\" CONTENT=\"0\">\n";

$metastring .= "<META NAME=\"RESOURCE-TYPE\" CONTENT=\"DOCUMENT\">\n";

$metastring .= "<META NAME=\"DISTRIBUTION\" CONTENT=\"GLOBAL\">\n";

$metastring .= "<META NAME=\"AUTHOR\" CONTENT=\"$sitename\">\n";

$metastring .= "<META NAME=\"COPYRIGHT\" CONTENT=\"Copyright (c) by $sitename\">\n";

$metastring .= "<META NAME=\"KEYWORDS\" CONTENT=\"Urban Exploration Pictures\">\n";

$metastring .= "<META NAME=\"DESCRIPTION\" CONTENT=\"".$row['text']."\n";

$metastring .= "<META NAME=\"ROBOTS\" CONTENT=\"INDEX, FOLLOW\">\n";

$metastring .= "<META NAME=\"REVISIT-AFTER\" CONTENT=\"1 DAYS\">\n";

$metastring .= "<META NAME=\"RATING\" CONTENT=\"GENERAL\">\n";

echo $metastring;

?>


Which outputs this

Code:
<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test Content - Lake Hopatcong - Raven Test</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<META HTTP-EQUIV="EXPIRES" CONTENT="0">
<META NAME="RESOURCE-TYPE" CONTENT="DOCUMENT">
<META NAME="DISTRIBUTION" CONTENT="GLOBAL">
<META NAME="AUTHOR" CONTENT="Raven Test">
<META NAME="COPYRIGHT" CONTENT="Copyright (c) by Raven Test">
<META NAME="KEYWORDS" CONTENT="Urban Exploration Pictures">
<META NAME="DESCRIPTION" CONTENT="<div align="center"><font size="5"><span>USB Webserver is nice little Web Server package that includes Apache, MySQL, PHP, and PHPMyAdmin. Much like XAMPP, NetServer and other popular Web Servers, the application is dockable to the system tray and services can be run either portability or installed as a system service. The menu items are available in the English language via the Settings tab. This application requires roughly 22.3MB for complete portable installation.</span></font></div>
<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
<META NAME="REVISIT-AFTER" CONTENT="1 DAYS">

<META NAME="RATING" CONTENT="GENERAL">


As you can see I need to remove those html tags.

Thanks
 
View user's profile Send private message
evaders99
Former Moderator in Good Standing



Joined: Apr 30, 2004
Posts: 3221

PostPosted: Sun Apr 12, 2009 8:39 pm Reply with quote

Code:


$metastring .= "<META NAME=\"DESCRIPTION\" CONTENT=\"".strip_tags($row['text'])."\n";

_________________
- Only registered users can see links on this board! Get registered or login! -

Need help? Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message Visit poster's website
misterpat







PostPosted: Sun Apr 12, 2009 8:47 pm Reply with quote

EXCELLENT! That worked. Thank you!

Now, anyone know how to cut the output down to say 25-30 words?
 
evaders99







PostPosted: Sun Apr 12, 2009 8:52 pm Reply with quote

Use substr
http://us.php.net/substr
 
misterpat







PostPosted: Sun Apr 12, 2009 9:49 pm Reply with quote

Ok, just tested this on a live server. I need to strip this also.

&nbsp;

Any ideas?
 
testy1
Involved
Involved



Joined: Apr 06, 2008
Posts: 484

PostPosted: Sun Apr 12, 2009 10:17 pm Reply with quote

http://au.php.net/manual/en/function.str-replace.php

Code:


$sCode = '&nbsp;Hi&nbsp;There&nbsp;How&nbsp;Are&nbsp;You';
$sCleanCode = str_replace('&nbsp;', ' ', $sCode );
 
View user's profile Send private message
evaders99







PostPosted: Sun Apr 12, 2009 11:38 pm Reply with quote

Or possibly
http://us2.php.net/manual/en/function.html-entity-decode.php
 
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Mon Apr 13, 2009 6:04 am Reply with quote

misterpat, although the input given so far is excellent, just realize that strip_tags() is quite aggressive. It will take out more text than what you may want IF that text has "<" in it. Probably not an issue for you, but just wanted to mention it.

If you are running RavenNuke(tm), you can use the check_html() function with the 'nohtml' option and it will do a better job of the strip. So, it might instead look something like this:

$sCleanCode = html_entity_decode(check_html($row['text'], 'nohtml'));

You might want to try it with and without the html_entity_decode(). You will want to test this with many different scenarios to make sure it is doing what you want.

_________________
Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message Visit poster's website
testy1







PostPosted: Mon Apr 13, 2009 6:19 am Reply with quote

never even thought of that Sad
 
misterpat







PostPosted: Mon Apr 13, 2009 7:13 am Reply with quote

Ok, Im going to try all of your sugestions today. Thanks for the input.

I will report back on my findings. Maybe someone else can use this code also.
 
misterpat







PostPosted: Mon Apr 13, 2009 6:42 pm Reply with quote

OK, I got something working here. Can some of You professionals look at my code and tell me if it needs cleaning up?



Code:
<?php


/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2002 by Francisco Burzi                                */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

if (stristr(htmlentities($_SERVER['PHP_SELF']), 'meta.php')) {
    Header('Location: ../index.php');
    die();
}

// Get functions
global $commercial_license, $sitename, $slogan, $row, $db, $sCode, $str, $count;

        $id = $_GET['pid'];
        $sql = 'SELECT text FROM nuke_pages WHERE pid=\'' . intval($id) . '\'';
        $result = $db->sql_query($sql);
        $row = $db->sql_fetchrow($result);

        $count = str_replace('&nbsp;', '', $row );
        $cCode = strip_tags($count[text]);
        $dCode = trim($cCode);
##################################################
# Include for Meta Tags generation               #
##################################################
$metastring = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="._CHARSET."\">\n";

$metastring .= "<META HTTP-EQUIV=\"EXPIRES\" CONTENT=\"0\">\n";

$metastring .= "<META NAME=\"RESOURCE-TYPE\" CONTENT=\"DOCUMENT\">\n";

$metastring .= "<META NAME=\"DISTRIBUTION\" CONTENT=\"GLOBAL\">\n";

$metastring .= "<META NAME=\"AUTHOR\" CONTENT=\"$sitename\">\n";

$metastring .= "<META NAME=\"COPYRIGHT\" CONTENT=\"Copyright (c) by $sitename\">\n";

$metastring .= "<META NAME=\"KEYWORDS\" CONTENT=\"Urban Exploration Pictures\">\n";

$metastring .= "<META NAME=\"DESCRIPTION\" CONTENT=\"".substr($dCode, 0, 135)."\">\n";

$metastring .= "<META NAME=\"ROBOTS\" CONTENT=\"INDEX, FOLLOW\">\n";

$metastring .= "<META NAME=\"REVISIT-AFTER\" CONTENT=\"1 DAYS\">\n";

$metastring .= "<META NAME=\"RATING\" CONTENT=\"GENERAL\">\n";



echo $metastring;
?>


Check out the nice page description it gives.

Code:
<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test Content - RavenNuke Kicks Ass - Raven Test</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<META HTTP-EQUIV="EXPIRES" CONTENT="0">
<META NAME="RESOURCE-TYPE" CONTENT="DOCUMENT">
<META NAME="DISTRIBUTION" CONTENT="GLOBAL">
<META NAME="AUTHOR" CONTENT="Raven Test">
<META NAME="COPYRIGHT" CONTENT="Copyright (c) by Raven Test">
<META NAME="KEYWORDS" CONTENT="Urban Exploration Pictures">
<META NAME="DESCRIPTION" CONTENT="USB Webserver is nice little Web Server package that includes Apache, MySQL, PHP, and PHPMyAdmin. Much like XAMPP, NetServer and other ">
<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">
<META NAME="REVISIT-AFTER" CONTENT="1 DAYS">
<META NAME="RATING" CONTENT="GENERAL">


I notice its not giving the generator meta tags. Is that drawn from a different file?
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©