[英]Add hyperlinks to words in php
I have found this function on buildinternet.com . 我在buildinternet.com上找到了此功能。
function tag_it($text) {
$text = preg_replace("/:(\w+):/", '<a href="http://www.example.com/page/$1/" title="$1" target="_blank">$1</a>',$text);
return $text;
}
What it does is adding hyperlinks to words enclosed in ":" and the problem is that it works only with single words and it doesn`t work with words that has a single quote. 它的作用是为包含在“:”中的单词添加超链接,问题是它仅适用于单个单词,而不适用于带有单引号的单词。
So if I do this, 所以如果我这样做
$test = "one :word1:, :two words:, :this is a phrase:, this has a single quote :don't: or :don't forget:.";
echo tag_it($test);
only to the :word1: will be added a hyperlink, the rest will be ignored. 仅向:word1:添加一个超链接,其余的将被忽略。
I don`t know too much php and I would appreciate if someone could make the function work with more than one word and with a single quote too. 我对PHP不太了解,如果有人可以使该函数使用多个单词并使用单引号,我将不胜感激。
Thank you! 谢谢!
Edit: 编辑:
So I have tried the following to add a different link to words enclosed in "#" 因此,我尝试了以下操作,以将不同的链接添加到“#”中包含的单词上
function tag_it($text) {
$out = preg_replace_callback(
"/:([^\:]+):/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', '', $m[1]);
$link = str_replace("'", "", $m[1]);
$link = str_replace(" ", "_", $link);
return '<a href="http://www.example.com/page/'.$link.'/" title="'.$linkText.'" target="_blank">'.$linkText.'</a>';
},
preg_replace_callback(
"/#([^\#]+)#/",
function($m1) {
$linkText1 = $m1[1];
$link1 = str_replace('"', '', $m1[1]);
$link1 = str_replace("'", "", $m1[1]);
$link1 = str_replace(" ", "_", $link1);
return '<a href="http://www.example.com/blog/'.$link1.'/" title="'.$linkText1.'" target="_blank">'.$linkText1.'</a>';
},
$text
)
);
return $out;
}
$test = "one :word:, #two words#, :this is a phrase:, this has a single quote :don:'t or :don't forget:.";
echo tag_it($test);
and i get this 我明白了
one word, two_words,_/" title="//www.example.com/blog/two_words/" title="two words" target="_blank">two words, " target="_blank">//www.example.com/blog/two_words/" title="two words" target="_blank">two words, this is a phrase, this has a single quote don't or don't forget:.
It seems to work for the 1st word enclosed in ":" and is trying to work with the words enclosed in "#" too but something is happening along the way and I can`t figure it out. 它似乎适用于“:”中包含的第一个单词,并且也尝试使用“#”中包含的单词,但是在此过程中发生了一些事情,我无法弄清楚。
Any tip is really appreciated. 任何提示都非常感谢。
Thank you! 谢谢!
将函数中的关键行更改为以下内容:
$text = preg_replace("/:([^:]+):/", '<a href="http://www.example.com/page/$1/" title="$1" target="_blank">$1</a>', $text);
将此/:(\\w+):/
更改为此/:([^:]+):/
Try this hope this simple one will help you out.. 尝试这种希望这一简单的方法可以对您有所帮助。
Regex: :([^\\:]+):
正则表达式::
:([^\\:]+):
:([^\\:]+):
it will match:
then all till not:
and then:
in end:([^\\:]+):
它将匹配:
则直到所有未:
然后:
在端
Try this code snippet here 在此处尝试此代码段
<?php
ini_set('display_errors', 1);
$test = "one :word1:, :two words:, :this is a phrase:, this has a single quote :don't: or :don't forget:.";
echo tag_it($test);
function tag_it($text)
{
$text = preg_replace("/:([^\:]+):/", '<a href="http://www.example.com/page/$1/" title="$1" target="_blank">$1</a>', $text);
return $text;
}
I'll let you finish it off, above are all valid, but leave you with broken links, so building on them, I'd do something like: 我会让你结束它,以上都是有效的,但是留下损坏的链接,因此在它们的基础上,我会做类似的事情:
function tag_it($text) {
$out = preg_replace_callback(
"/:([^:]+):/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', "", $m[1]);
$link = str_replace("'", "", $m[1]);
return '<a href="http://www.example.com/page/'.$link.'/" title="'.$linkText.'" target="_blank">'.$linkText.'</a>';
},
$text);
return $out;
}
You'll need to finish it off to replace spaces with - or _ or something but should be easy enough to work it out. 您需要完成将其替换为-或_之类的空格,但是应该很容易解决。
To answer to my " Edit: ", I have figured out the problem. 为了回答我的“ Edit: ”,我已经解决了这个问题。
Here is the full function 这是全部功能
function tag_it($text) {
$out = preg_replace_callback(
"/:([^\:]+):/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', '', $m[1]);
$link = str_replace("'", "", $m[1]);
$link = str_replace(" ", "_", $link);
return '<a href="http://www.example.com/page/'.$link.'/" title="'.$linkText.'" target="_blank">'.$linkText.'</a>';
},$text
);
$out = preg_replace_callback(
"/#([^\#]+)#/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', '', $m[1]);
$link = str_replace("'", "", $m[1]);
$link = str_replace(" ", "_", $link);
return '<a href="http://www.example.com/blog/'.$link.'/" title="'.$linkText.'" target="_blank">'.$linkText.'</a>';
},$out
);
$out = preg_replace_callback(
"/@([^\@]+)@/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', '', $m[1]);
$link = str_replace("'", "", $m[1]);
$link = str_replace(" ", "_", $link);
return '<a href="http://www.example.com/article/'.$link.'/" title="'.$linkText.'" target="_blank">'.$linkText.'</a>';
},$out
);
return $out;
}
$test = "one :word:, #two words#, @this is a phrase@, this has a single quote @don't@ or :don't forget:.";
echo tag_it($test);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.