Shoutbox

PHP help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP help (/showthread.php?tid=51184)

PHP help by Ezra on 09-30-2005 at 05:03 PM

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.


RE: PHP help by J-Thread on 09-30-2005 at 05:24 PM

$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)


RE: PHP help by Ezra on 09-30-2005 at 05:37 PM

Doesn't work with me :(

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


RE: PHP help by L. Coyote on 09-30-2005 at 06:10 PM

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
RE: PHP help by Ezra on 09-30-2005 at 06:16 PM

Wonderfull preg_replace works with _'s :)


RE: PHP help by WDZ on 09-30-2005 at 06:35 PM

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:
RE: PHP help by Ezra on 09-30-2005 at 06:55 PM

Oh, is there also any way to remove the _ from the visible part of the link?


RE: PHP help by brian on 09-30-2005 at 07:14 PM

Err, unless you encode it which makes it %05 or something, _ look better. =p


RE: PHP help by Ezra on 09-30-2005 at 08:38 PM

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>


RE: PHP help by J-Thread on 09-30-2005 at 08:58 PM

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;)
RE: PHP help by Ezra on 09-30-2005 at 10:15 PM

It's great :), fantastic, I never could have made that myself...

If I understand correct, If I use [[wiki]blaa] it goes to the english version?


RE: PHP help by WDZ on 10-01-2005 at 04:03 AM

quote:
Originally posted by J-Thread
function wiki_link($input) {
......
Holy crap, what was that? :shocked:

I would have just used the 'e' modifier with preg_replace(). :p

http://php.net/manual/en/reference.pcre.pattern.modifiers.php
quote:
Originally posted by PHP Manual
If this modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string.

RE: PHP help by J-Thread on 10-01-2005 at 08:22 AM

My PERL Regex aren't that good... At least I'm sure the replacement of _ isn't possible with a simple regular expression. How would you do that with your preg function?

By the way, Leo's solution with the preg_replace does work, but only if there is one link to replace. Try this:

code:
$input = "[[wiki:nl]RAM] and [[wiki:en]Does_this_work]";

Off course it should give 2 links, but it gives:
code:
<a href="http://nl.wikipedia.org/wiki/RAM] and [[wiki:en]Does_this_work">RAM] and [[wiki:en]Does_this_work</a>

My function does work there. It also works if you don't specify a language. So [[wiki]Test] is just replaced by http://www.wikipedia.org/wiki/Test :-)

When the string is incorrect there might be an infinite loop by the way... So it could be better.

But how would you do it with preg_replace? I can learn from it too :-)
RE: PHP help by -dt- on 10-01-2005 at 09:29 AM

code:

$string = preg_replace('/\[\[wiki(?:\:|)([a-zA-Z]+|)\]([a-zA-z0-9]+)\]/e', 'defL("$1","$2")',$string);

function defL($lang,$title){
if($lang=='')$lang='en';
return "<a href='http://$lang.wikipedia.org/wiki/$title'>$title</a>";
}


that works fine :P
RE: PHP help by J-Thread on 10-01-2005 at 11:08 AM

code:
$string = preg_replace('/\[\[wiki(?:\:|)([a-zA-Z]+|)\]([a-zA-z0-9]+)\]/e', 'defL("$1","$2")',$string);

function defL($lang,$title){
if($lang=='')$lang='en';
return "<a href='http://$lang.wikipedia.org/wiki/$title'>" . str_replace("_", " ", $title) . "</a>";
}

That also replaces the _ to spaces. Ok I didn't knew something like this did exist, really interesting. This is the final one, and is the best option I think.

Maybe replace $lang='en' by $lang='www', so people are sended to the default language of wikipedia, and maybe to there own language if wikipedia introduces a language check...
RE: RE: PHP help by -dt- on 10-01-2005 at 02:11 PM

quote:
Originally posted by J-Thread
code:
$string = preg_replace('/\[\[wiki(?:\:|)([a-zA-Z]+|)\]([a-zA-z0-9]+)\]/e', 'defL("$1","$2")',$string);

function defL($lang,$title){
if($lang=='')$lang='en';
return "<a href='http://$lang.wikipedia.org/wiki/$title'>" . str_replace("_", " ", $title) . "</a>";
}

That also replaces the _ to spaces. Ok I didn't knew something like this did exist, really interesting. This is the final one, and is the best option I think.

Maybe replace $lang='en' by $lang='www', so people are sended to the default language of wikipedia, and maybe to there own language if wikipedia introduces a language check...




code:
$string = preg_replace('/\[\[wiki(?:\:|)([a-zA-Z]+|)\]([a-zA-z0-9_]+)\]/e', 'defL("$1","$2")',$string);

function defL($lang,$title){
if($lang=='')$lang='en';
$title1 = str_replace("_", " ", $title);
return "<a href='http://$lang.wikipedia.org/wiki/$title'>$title1</a>";
}


looks neater :P  oh and i also added the _ to the regexp capture part.
RE: PHP help by L. Coyote on 10-01-2005 at 04:16 PM

quote:
Originally posted by WDZ
quote:
Originally posted by J-Thread
function wiki_link($input) {
......
Holy crap, what was that? :shocked:

I would have just used the 'e' modifier with preg_replace(). :p

http://php.net/manual/en/reference.pcre.pattern.modifiers.php
quote:
Originally posted by PHP Manual
If this modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string.

Damn! I spent a while reading that yesterday and didn't see it... That's really time saving! (Y)
RE: PHP help by Ezra on 10-01-2005 at 05:47 PM

quote:
Originally posted by -dt-

code:
$string = preg_replace('/\[\[wiki(?:\:|)([a-zA-Z]+|)\]([a-zA-z0-9_]+)\]/e', 'defL("$1","$2")',$string);

    function defL($lang,$title){
    if($lang=='')$lang='en';
    $title1 = str_replace("_", " ", $title);
    return '<a href="http://'.$lang.'.wikipedia.org/wiki/'.$title.'">'.$title1.'</a>';
    }


Found a little error, fixed it

Instead of using the parts defined in the tag it made a link like this:
<a href="$lang.wikipedia.org/wiki/$title">$title1</a>
RE: PHP help by J-Thread on 10-01-2005 at 06:03 PM

That isn't an error is it? I'd rather prefer:

code:
return "<a href=\"http://" . $lang . ".wikipedia.org/wiki/" . $title . "\">" . $title1 . "</a>";

But thats just style..
RE: RE: PHP help by -dt- on 10-02-2005 at 05:14 AM

quote:
Originally posted by Ezra
quote:
Originally posted by -dt-

code:
$string = preg_replace('/\[\[wiki(?:\:|)([a-zA-Z]+|)\]([a-zA-z0-9_]+)\]/e', 'defL("$1","$2")',$string);

    function defL($lang,$title){
    if($lang=='')$lang='en';
    $title1 = str_replace("_", " ", $title);
    return '<a href="http://'.$lang.'.wikipedia.org/wiki/'.$title.'">'.$title1.'</a>';
    }


Found a little error, fixed it

Instead of using the parts defined in the tag it made a link like this:
<a href="$lang.wikipedia.org/wiki/$title">$title1</a>


WTf please tell me how it would return a link like that

my code
code:

$string = preg_replace('/\[\[wiki(?:\:|)([a-zA-Z]+|)\]([a-zA-z0-9_]+)\]/e', 'defL("$1","$2")',$string);


function defL($lang,$title){
if($lang=='')$lang='en';
$title1 = str_replace("_", " ", $title);
return "<a href='http://$lang.wikipedia.org/wiki/$title'>$title1</a>";
}


because I use " to contain the string which will parse any varible in that string unlike its brother '

:-/ so im not getting how its an error...
RE: PHP help by Ezra on 10-02-2005 at 12:59 PM

Ah, that's why :P, I changed the " for ' :P.

But it works now :)