• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

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

#1
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.
 
#2
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 = "";
    }
 
#3
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.
 
#4
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.
 
S

Stuart Leyland

Guest
#5
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 :)
 
#6
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.
 
#7
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,962
Messages
673,240
Members
89,015
Latest member
loxioalix