What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » PHP help

Pages: (3): « First [ 1 ] 2 3 » Last »
PHP help
Author: Message:
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. PHP help
I'm trying to mod my blog a little, I want to add a function that automaticly adds Wikipedia links.

So when i type [[wiki]RAM] It makes it <a href="http://en.wikipedia.org/wiki/RAM">RAM</a>

And maybe even support for different languages like

[[wiki:nl]RAM] for http://nl.wikipedia.org/wiki/RAM and
[[wiki:en]RAM] for http://en.wikipedia.org/wiki/RAM

I'm not really a hero with advanced string replacers :P, so any help is appreciated :)

BTW: I also noticed that wikipedia has _ as spaces in links, maybe strip those out and make them normal spaces in the part that shows on the page.
[Image: 1-0.png]
             
09-30-2005 05:03 PM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: PHP help
$input = ereg_replace("\[\[wiki\]([[:alpha:]]+)\]", "<a href=\"http://www.wikipedia.org/wiki/\\1\">\\1</a>", $input);

That's your first question. With languages it is:

$input = ereg_replace("\[\[wiki:([[:alpha:]]+)\]([[:alpha:]]+)\]", "<a href=\"http://\\1.wikipedia.org/wiki/\\2\">\\2</a>", $input);

(Y)

This post was edited on 09-30-2005 at 05:25 PM by WDZ.
09-30-2005 05:24 PM
Profile E-Mail PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: PHP help
Doesn't work with me :(

Some how it doesn't work with _ and there are _ in a lot of links

This post was edited on 09-30-2005 at 05:45 PM by Ezra.
[Image: 1-0.png]
             
09-30-2005 05:37 PM
Profile PM Web Find Quote Report
L. Coyote
Senior Member
****

Avatar
Captain Obvious

Posts: 981
Reputation: 49
39 / Male / Flag
Joined: Aug 2004
Status: Away
RE: PHP help
quote:
Originally posted by Ezra
I'm trying to mod my blog a little, I want to add a function that automaticly adds Wikipedia links.

So when i type [[wiki]RAM] It makes it <a href="http://en.wikipedia.org/wiki/RAM">RAM</a>
Preg is better, IMO.

code:
$string = preg_replace("/\[\[wiki\](.+)\]/i", '<a href="http://en.wikipedia.org/wiki/$1">$1</a>', $string);

quote:
And maybe even support for different languages like

[[wiki:nl]RAM] for http://nl.wikipedia.org/wiki/RAM and
[[wiki:en]RAM] for http://en.wikipedia.org/wiki/RAM

I'm not really a hero with advanced string replacers :P, so any help is appreciated :)
code:
$string = preg_replace("/\[\[wiki:([a-z]{2})\](.+)\]/i", '<a href="http://$1.wikipedia.org/wiki/$2">$2</a>', $string);

Not sure, didn't test it. Variable name should be changed to the variable name you use for your post text. (Y)

Edit: corrected a lot of typos :p


Edit 2: @ WDZ => preg_ > ereg/i :refuck:

Edit 3: can be quite buggy if you use many tags in the same post... cbb to fix it :p

This post was edited on 09-30-2005 at 10:06 PM by L. Coyote.

Hack, hack, hack!
Finally became a Systems Analyst! :spam:

09-30-2005 06:10 PM
Profile PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: PHP help
Wonderfull preg_replace works with _'s :)
[Image: 1-0.png]
             
09-30-2005 06:16 PM
Profile PM Web Find Quote Report
WDZ
Former Admin
*****

Avatar

Posts: 7106
Reputation: 107
– / Male / Flag
Joined: Mar 2002
RE: PHP help
quote:
Originally posted by Ezra
Wonderfull preg_replace works with _'s :)
ereg_replace() works with them too FYI, but J-Thread didn't make his code support them. One small change would have fixed it. :p

Anyways, preg_replace() is indeed better. :banana:
09-30-2005 06:35 PM
Profile PM Web Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: PHP help
Oh, is there also any way to remove the _ from the visible part of the link?
[Image: 1-0.png]
             
09-30-2005 06:55 PM
Profile PM Web Find Quote Report
brian
Senior Member
****

Avatar

Posts: 819
Reputation: 43
– / Male / –
Joined: Sep 2004
RE: PHP help
Err, unless you encode it which makes it %05 or something, _ look better. =p
09-30-2005 07:14 PM
Profile PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: PHP help
No, that's not what I mean :P

I mean that it's like this

[[wiki:nl]Random_acces_memory] becomes:

<a href="http://nl.wiki.org/wiki/Random_acces_memory">Random Acces Memory</a>

[Image: 1-0.png]
             
09-30-2005 08:38 PM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: PHP help
code:
function wiki_link($input) {
   $i = 0;
   $output = "";
   while($i < strlen($input)) {
      $searchfor = "[[wiki";
      if(substr($input, $i, strlen($searchfor)) == $searchfor) {
         if($input{$i+strlen($searchfor)} == ":") {
            $next = strpos($input, "]", $i);
            $lang = substr($input, $i+strlen($searchfor)+1, $next - ($i+strlen($searchfor)) - 1);
            $next2 = strpos($input, "]", $next + 1);
            $thestr = substr($input, $next + 1, $next2 - $next - 1);
            $output .= "<a href=\"http://" . $lang . ".wikipedia.org/wiki/" . $thestr . "\">" . str_replace("_", " ", $thestr) . "</a>";
            $i = $next2 + 1;
         }
         elseif($input{$i+strlen($searchfor)} == "]") {
            $next = strpos($input, "]", $i+strlen($searchfor)+1);
            $thestr = substr($input, $i+strlen($searchfor) + 1, $next - ($i+strlen($searchfor)) - 1);
            $output .= "<a href=\"http://www.wikipedia.org/wiki/" . $thestr . "\">" . str_replace("_", " ", $thestr) . "</a>";
            $i = $next + 1;
         }
         else {
            $output .= $searchfor;
            $i += strlen($searchfor);
         }
      }
      else {
         $output .= $input{$i};
         $i++;
      }
   }
   return $output;
}

May look a bit complicated, but i don't think there is a better option, and it works so...have fun with it;)
09-30-2005 08:58 PM
Profile E-Mail PM Find Quote Report
Pages: (3): « First [ 1 ] 2 3 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On