PHP help |
Author: |
Message: |
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
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 , 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.
|
|
09-30-2005 05:03 PM |
|
|
J-Thread
Full Member
Posts: 467 Reputation: 8
– / / –
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);
This post was edited on 09-30-2005 at 05:25 PM by WDZ.
|
|
09-30-2005 05:24 PM |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
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.
|
|
09-30-2005 05:37 PM |
|
|
L. Coyote
Senior Member
Captain Obvious
Posts: 981 Reputation: 49
39 / /
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 , 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.
Edit: corrected a lot of typos
Edit 2: @ WDZ => preg_ > ereg/i
Edit 3: can be quite buggy if you use many tags in the same post... cbb to fix it
This post was edited on 09-30-2005 at 10:06 PM by L. Coyote.
Hack, hack, hack!
Finally became a Systems Analyst!
|
|
09-30-2005 06:10 PM |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
Joined: Mar 2003
|
O.P. RE: PHP help
Wonderfull preg_replace works with _'s
|
|
09-30-2005 06:16 PM |
|
|
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
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.
Anyways, preg_replace() is indeed better.
|
|
09-30-2005 06:35 PM |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
Joined: Mar 2003
|
O.P. RE: PHP help
Oh, is there also any way to remove the _ from the visible part of the link?
|
|
09-30-2005 06:55 PM |
|
|
brian
Senior Member
Posts: 819 Reputation: 43
– / / –
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 |
|
|
Ezra
Veteran Member
Forgiveness is between them and God
Posts: 1960 Reputation: 31
37 / /
Joined: Mar 2003
|
O.P. RE: PHP help
No, that's not what I mean
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>
|
|
09-30-2005 08:38 PM |
|
|
J-Thread
Full Member
Posts: 467 Reputation: 8
– / / –
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 |
|
|
Pages: (3):
« First
[ 1 ]
2
3
»
Last »
|
|