Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 20th, 2003, 11:08 AM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default strip html tags & PHP

Hi All,

I want to create a user friendly back end in which a person can update her personal information without knowledge about html tags.

If i used strip_tags function to strip all html tags, so that the form looks very clean. The trouble is that when a user edit her information and hit update button, all the html format will be gone.

So what do i do to reserve all html tags? And if she wants to type in xxx with a url, then how do i know to convert it to a link. For example: yahoo, http://www.yahoo.com.

Any suggestions would be greatly appreciated

trang

Reply With Quote
  #2 (permalink)  
Old October 20th, 2003, 12:33 PM
Registered User
 
Join Date: Sep 2003
Location: Carlsbad, CA, .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What you're describing sounds mildly like what a Wiki or other web content management systems do. They generate the HTML behind the scenes, and present a simplified language to the user for use.

For example, a lot of web bulletin board systems use some sort of code you can put in a post to link in an image or make a bulleted list or whatever. You could emulate this behavior by creating a notation for links (and any other HTML-type things you want to have available) and then writing code that translates that into the appropriate HTML when it is being displayed.

Regards,
Jake.
Reply With Quote
  #3 (permalink)  
Old October 20th, 2003, 01:10 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, to reserve all tags, you'd simply NOT use strip_tags. If you'd like to have a set of allowable HTML tags, the easiest thing to do is to parse out all the tags that you want to keep and strip the rest. Alternatively, you can implement a similar system to the "forum code" that you see here, where people use HTML-esque tags using square brackets instead of angle brackets. For example, [brace]b]bold words[brace]/b] instead of <b>bold words</b>.

In that case, you can simply strip ALL html tags, and use preg_replace() to replace the accepted forum code tags with HTML tags.

Converting URLs to links is also just a matter of regular expression search and replace.

There are lots of examples for doing these types of conversions online. Try looking at the function preg_replace() for an idea of where to start.
  http://www.php.net/preg_replace


Another method would be to use the preg_replace_callback() function to manage all your HTML links. A callback function is a function that gets called when some event happens. In preg_replace_callback, your function is called whenever a pattern is matched. The callback function takes the place of the replacement pattern. It's only parameter is the array of strings that matched your pattern.


For example, suppose I had this callback function:

function linkify($matches)
{
    return "<a href=\"{$matches[0]}\">{$matches[0]}</a>";
}

This function clearly expects the entire pattern to match a URL. We can use this to create a hyperlink out of a regularly-typed URL.

Here's a sample program for you to play with:

<?php

function linkify($matches)
{
    return "<a href=\"{$matches[0]}\">{$matches[0]}</a>";
}

$pattern = '!\bhttps?://([\w\-]+\.)+[a-zA-Z]{2,3}(/(\S+)?)?\b!';

/* Pattern above matches any link starting with http:// or https://,
 * followed by one or more groups of at least one word-character (\w)
 * and a period (\.), followed by either two or three letters ([a-zA-Z]),
 * followed by an optional slash, and any number of optional non-whitespace
 * characters.
 * The \b assertions force the pattern to begin and end at a "word boundary",
 *
 *
 * This is by no means a "complete" URL matching pattern, but it's short and
 * matches lots of URLs, including:
 * http://www.bigaction.org
 * http://p2p.wrox.com/topic.asp?TOPIC_ID=5482
 * https://login.yahoo.com/config/login
 *
 */

$textarea = isset($_POST['some_text'])? $_POST['some_text'] : "
Feel free to type some sample text here, including some links.
For example:
  http://www.bigaction.org
  http://www.bigaction.org/
  https://login.yahoo.com/config/login
  http://p2p.wrox.com/topic.asp?TOPIC_ID=5482
";

echo <<<EOF
<form method="post" action="{$_SERVER['PHP_SELF']}">
  <textarea name="some_text" cols="60" rows="8">{$textarea}</textarea>
  <br />
  <input type="submit" name="submit_button" value="Linkify!" />
</form>

EOF;

if (isset($_POST['some_text']))
{
    echo "\n";
    echo "<pre>\n";
    echo preg_replace_callback($pattern, 'linkify', $_POST['some_text']);
    echo "</pre>\n";
}


?>


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #4 (permalink)  
Old October 20th, 2003, 02:52 PM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much Nik. You are so kind.

1. I just learned valuable linkify from you. It's very cool.
2. I have tried to used HTML-esques tags and then replace with html tags. It works fine but not userfriendly. I have some users with old traditions and they hate seeing weird tags. They prefer blank text.
Let me give you more info: right now there are some pages were done in html. I transfered the datata (including html tags) into the database and display back. This part is done.
Now at the updating form, data is shown with html tags, which bugging users. If i replace all <> with []in a form and replace back when the form is submited, which are easy, but they are not happy. They want blank text. So I'm stuck. Do you have another alternative?
For example:
**************************************************
[ul][li]hello[/li][/ul][p][a href="http://www.yahoo.com"]WROX[/a][/p]
************************************************** *****
When they look at the above line, they got upset. They don't know where the real text is.

Thank you so much
td

Reply With Quote
  #5 (permalink)  
Old October 20th, 2003, 03:33 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Quote:
quote:
When they look at the above line, they got upset. They don't know where the real text is.
I know what you're going through!

I have a client which I need to come up with some kind of user-friendly interface for posting content on their website.††I have considered going with a more complex WYSIWYG interface.††It can be done with JavaScript directly in the browser and the look and feel be designed to resemble something like Microsoft Word or FrontPage.††There are a few tutorials and complete scripts already out there.††Going with this approach will allow the user to post plain text if they so desire or format it.††

Some of the tutorials that I've seen also tend to rely on Microsoft only technologies..

I have also considered a file upload form in which the user would simply use FrontPage or Mozilla/NS Composer or whatever to create the HTML file and then upload the file and manipulate it to fit the template using PHP.

Both options are pretty complex to write from scratch.
But its another approach to consider.

http://www.google.com/search?q=WYSIWYG+JavaScript

Hope that helps!
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #6 (permalink)  
Old October 20th, 2003, 04:30 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It sounds like you have different translation models depending on where you're displaying the text.

When displaying it as a web page, you want to keep the HTML. When displaying it as plain text to a user, you *don't* want HTML tags in there. The second scenario might cause problems if you're displaying the text in some sort of input field where a user can edit the text and resubmit it to the site -- you don't want all of the original formatting (bold, italics, etc) removed when the user submits the new version.

So basically, you need to come up with a way of preserving the original text and translating it on-the-fly using the appropriate translation algorithm, or storing all the different translations of the original text.

For example, let's take a look at the p2p forums. I've thought for a while that the p2p forums should remove forum code from the plain-text emails it sends to forum subscribers. Assuming Wrox implements this, we'll have three translation models: straight passthru, HTML convert, and plain-text convert.

straight passthru: This means that the "translation" does nothing -- the original text submitted by the author is returned.

HTML convert: This means that all forum code is converted to HTML, and any special HTML characters in the original text are converted to their entity equivalents; e.g. < is converted to &lt;. This is used for displaying a thread in a user's browser or as the body of an HTML message.

Plain text convert: This means that all forum code is stripped from the message to avoid cluttering the actual text. This could be used for text-based browsers or plain-text email subscribers.



I suggest you decide how many translation models you need, and write each one separately as a function that accepts the original text and returns the translated text. That way, your site can always call some sort of translation callback function to generate the appropriate text for the context of the application.


I hope this makes sense; I tried to keep things high-level, but I might have over-simplified.


, but convert the forum code to HTML for subscribers who prefer HTML emails.

The original post should be stored as entered by the user.

Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #7 (permalink)  
Old October 21st, 2003, 10:28 AM
Authorized User
 
Join Date: Oct 2003
Location: , , .
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you everyone for giving me your valuable ideas especially Nik.††I really appreciate your detailed explanations for everything.††I will come back with more questions.

Take care

td
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
strip html tags with xslt smiter XSLT 4 June 19th, 2012 08:34 AM
strip html from spider script results mcalcagno PHP How-To 0 January 31st, 2005 08:14 AM
Strip HTML with ASP Question ryanpatrick Classic ASP Professional 1 January 17th, 2005 07:15 AM
remove html tags lucian Dreamweaver (all versions) 1 November 14th, 2004 02:25 PM
PHP & Strip tags trangd Pro PHP 1 October 20th, 2003 12:23 PM



All times are GMT -4. The time now is 07:46 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.