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. RE: PHP help
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?
[Image: 1-0.png]
             
09-30-2005 10:15 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 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.
10-01-2005 04:03 AM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: PHP help
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 :-)
10-01-2005 08:22 AM
Profile E-Mail PM Find Quote Report
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
35 / Male / Flag
Joined: Mar 2004
RE: PHP help
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

This post was edited on 10-01-2005 at 09:32 AM by -dt-.
[Image: dt2.0v2.png]      Happy Birthday, WDZ
10-01-2005 09:29 AM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: PHP help
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...

This post was edited on 10-01-2005 at 11:08 AM by J-Thread.
10-01-2005 11:08 AM
Profile E-Mail PM Find Quote Report
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
35 / Male / Flag
Joined: Mar 2004
RE: RE: PHP help
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.

This post was edited on 10-01-2005 at 02:15 PM by -dt-.
[Image: dt2.0v2.png]      Happy Birthday, WDZ
10-01-2005 02:11 PM
Profile PM Web Find Quote Report
L. Coyote
Senior Member
****

Avatar
Captain Obvious

Posts: 981
Reputation: 49
38 / Male / Flag
Joined: Aug 2004
Status: Away
RE: PHP help
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)

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

10-01-2005 04:16 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
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>

This post was edited on 10-01-2005 at 05:48 PM by Ezra.
[Image: 1-0.png]
             
10-01-2005 05:47 PM
Profile PM Web Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: PHP help
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..
10-01-2005 06:03 PM
Profile E-Mail PM Find Quote Report
-dt-
Scripting Contest Winner
*****

Avatar
;o

Posts: 1819
Reputation: 74
35 / Male / Flag
Joined: Mar 2004
RE: RE: PHP help
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...

This post was edited on 10-02-2005 at 05:15 AM by -dt-.
[Image: dt2.0v2.png]      Happy Birthday, WDZ
10-02-2005 05:14 AM
Profile PM Web 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