(PHP) I have no way of explaining it in one line. Read thread plz.

Glaanieboy

OSNN Veteran Addict
Joined
6 Mar 2002
Messages
2,628
Here is part of the code for my shoutbox:
Code:
$string = htmlspecialchars($fetchrow[1]);
$part = explode(" ", $string);
$ttl_words = count($part);
$smilies = array(";)" => "<img src=\"../images/smilies/wink.gif\" border=\"0\">",
 "B)" => "<img src=\"../images/smilies/cool.gif\" border=\"0\">",
 ":)" => "<img src=\"../images/smilies/smile.gif\" border=\"0\">");
$initial = array("/\[url=(.+?)\](.+?)\[\/url\]/", "/\[url\](.+?)\[\/url\]/");
$replace = array("<a href=$1>$2</a>", "<a href=$1>$1</a>");
for ($X=1;$X<=$ttl_words;$X++)
{
  $Y = $X-1;
  if (strlen($part[$Y]) > 30)
  {
   $part[$Y] = substr($part[$Y], 0, 30)."...";
  }
}
$string = implode(" ", $part);
$changebr = array("&lt;BR&gt;" => "<BR>", "****" => "f00k");
$string = strtr($string, $smilies);
$string = preg_replace($initial, $replace, $string);
print strtr($string, $changebr);

(for clarification: the text is being stored in the db as raw text, so everything a user types, will be stored in the db. All of the formatting will be performed when the text is being read)

This afternoon I placed the $initial and $replace arrays together with a preg_replace() function for [URL(=bla)}bla[/URL] parsing. But then I am getting into trouble with my long words checker (in the for... loop). As you can see, the $string is first being explode()d in $parts and each part is being checked if it's a long part (longer than 30 chars), if so, it is trimmed.
So far, so good. The trouble started when I put in the checker. My long word checker cons...m/bla/bla]loooooooooooooooooooooooooooooooong are being trimmed. Then I tried putting the preg_replace after the long word checker, but that also gives trouble with the '<a href=http://www.bla.com/bla/bla>looooooooooooooooooooooooooooooooong</a>, that is also longer than 30 chars.
How can I solve this? As a temporary workaround I have set the maximum number of characters for a word to 60, but I want to have a more professional method.
 
I have a url checker on my new shoutbox, which uses this code
BTW I hope the damn auto-parse URLs doesnt screw this (ah turned it off with tickbox, Glaanie you might want to try and fix that as well in your post, I would try to fix it for you but I cant really see the URL tags fer sht)

heres my code anyways
Code:
if($_POST['website'] != "website") {
        if(eregi("^http://", $_POST['website'])) {
            $shout_site = $_POST['website'];
        } else {
            $shout_site = "http://".$_POST['website'];
        }
    } else {
        $shout_site = "";
    }
 
Thanks. But that's not entirely what I am looking for. I am looking for a method that allows people to post tags in the messages itself. I wa...s don't understand what they mean on php.net.
 
I think what you have to do is test the string for an occurance of and then its closing tag, and then... the section in there and see how they do it.
 
From what I gather, you want to be able to allow people to use the tag to be able to put addresses in...ta containing the BBCode tags (ie: [url] and ) into a database. At this point you should not change anything, just treat it as if it is a normal piece of text. It is then on the page that you use to view the data from the database that you make the link readable and that is achieved by using the preg_replace() function of PHP.

Simply copy and paste this code into the page that you are using to display the messages:

PHP:
        $message = preg_replace("/(^|\s)(http:\/\/\S+)/si", "\\1<a href=\"\\2\">\\2</a>", $message); 
        $message = preg_replace("/(^|\s)(www\.\S+)/si", "\\1<a href=\"http://\\2\">\\2</a>", $message); 
        $message = preg_replace("/\[url\](http|https|ftp)(:\/\/\S+?)\[\/url\]/si", "<a href=\"\\1\\2\" target=\"_blank\">\\1\\2</a>", $message); 
        $message = preg_replace("/\[url\](\S+?)\[\/url\]/si", "<a href=\"http://\\1\" target=\"_blank\">\\1</a>", $message); 
        $message = preg_replace("/\[url=(http|https|ftp)(:\/\/\S+?)\](.*?)\[\/url\]/si", "<a href=\"\\1\\2\" target=\"_blank\">\\3</a>", $message); 
        $message = preg_replace("/\[url=(\S+?)\](\S+?)\[\/url\]/si", "<a href=\"http://\\1\" target=\"_blank\">\\2</a>", $message);

Note: I take no credit at all for this script, it was taken from another website. Unfortunately I do not remember the address.

I hope that this will help you :)
 
Hey, thanks! I am definately going to play with it, but unfortunately I am going out of town for a few days.
One last (not important) note, I am not going to implement it in my revised shoutbox, but I will use it in my yet to be devoloped messageboard system. Just to prevent that someone is looking for it on my shoutbox, only to find out it doesn't work.
 
Glaanieboy, use Google to find some tut or something for Perl Regular Expressions. Thats what preg_ uses for the pattern to match
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back