2017-05-04 142 views
0

我在buildinternet.com上发现了此功能。在php中添加超文本链接

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; 
} 

它的作用是添加超链接到包含在词“:”而问题是,它与单个词只适用,它不`吨与具有单引号的话工作。

所以,如果我这样做,

$test = "one :word1:, :two words:, :this is a phrase:, this has a single quote :don't: or :don't forget:."; 
echo tag_it($test); 

仅对:字词1:将添加一个超链接,其余的将被忽略。

我不知道太多的PHP,我会很感激,如果有人可以使这个功能与多个单词和单引号也工作。

谢谢!

编辑:

所以我曾尝试以下,以一个不同的链接添加到

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

括在 “#” 字和我得到这个

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:. 

似乎为包含在“:”中的第一个单词工作,并试图使用“#”中包含的单词进行工作,但一路上正在发生一些事情,我无法通过把它弄出来。

任何提示真的很感激。

谢谢!

回答

1

我让你完成它,上面都是有效的,但离开你断开的链接,所以建立他们,我会做这样的事情:

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; 
} 

你需要完成它重新放置空格 - 或_或其他东西,但应该很容易解决。

+0

我正要询问如何从href属性替换空间,我不认为有人会注意到。你的解决方案工作的很好,我只需要添加这一行'$ link = str_replace('','_',$ link);'替换空格。非常感谢。 –

1

变更功能的关键线以下:

$text = preg_replace("/:([^:]+):/", '<a href="http://www.example.com/page/$1/" title="$1" target="_blank">$1</a>', $text); 
1

改变这个/:(\w+):/这个/:([^:]+):/

1

试试这个希望这个简单的人会帮助你..

正则表达式::([^\:]+):

:([^\:]+):它将匹配:然后直到所有未:然后:在结束

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; 
} 
0

要回答我的“编辑:”,我已经找到了问题。

这里是全功能

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 [email protected], this has a single quote @don'[email protected] or :don't forget:."; 
echo tag_it($test);