PHP help |
Author: |
Message: |
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
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?
|
|
09-30-2005 10:15 PM |
|
|
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
Joined: Mar 2002
|
RE: PHP help
quote: Originally posted by J-Thread
function wiki_link($input) {
......
Holy crap, what was that?
I would have just used the 'e' modifier with preg_replace().
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 |
|
|
J-Thread
Full Member
Posts: 467 Reputation: 8
– / / –
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 |
|
|
-dt-
Scripting Contest Winner
;o
Posts: 1819 Reputation: 74
36 / /
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
This post was edited on 10-01-2005 at 09:32 AM by -dt-.
Happy Birthday, WDZ
|
|
10-01-2005 09:29 AM |
|
|
J-Thread
Full Member
Posts: 467 Reputation: 8
– / / –
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 |
|
|
-dt-
Scripting Contest Winner
;o
Posts: 1819 Reputation: 74
36 / /
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 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-.
Happy Birthday, WDZ
|
|
10-01-2005 02:11 PM |
|
|
L. Coyote
Senior Member
Captain Obvious
Posts: 981 Reputation: 49
39 / /
Joined: Aug 2004
Status: Away
|
RE: PHP help
Hack, hack, hack!
Finally became a Systems Analyst!
|
|
10-01-2005 04:16 PM |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
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.
|
|
10-01-2005 05:47 PM |
|
|
J-Thread
Full Member
Posts: 467 Reputation: 8
– / / –
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 |
|
|
-dt-
Scripting Contest Winner
;o
Posts: 1819 Reputation: 74
36 / /
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-.
Happy Birthday, WDZ
|
|
10-02-2005 05:14 AM |
|
|
Pages: (3):
« First
«
1
[ 2 ]
3
»
Last »
|
|