2013-04-24 107 views
3

我有一个字符串,它看起来像这样:字符串替换多个值

喇嘛喇嘛%内容十分重要%BLA BLA%内容十分重要%

有没有办法只能更换前两个“%”(或最后两个),这样我就可以得到一个输出:

Bla bla <a href='link1'>yada yada</a> bla bla %yada yada% 

而且,如果需要的话,最后的两个“%”,因此它输出:

Bla bla <a href='link1'>yada yada</a> bla bla <a href='link2'>yada yada</a> 

我不知道如何区分前两个和最后两个,所以如果我想,我可以用链接替换第一个或最后两个标记“%” 。 我使用的是PHP。在此先感谢

问候

+0

由于链接标识符是'%',它可能会以很多不同的方式导致很多错误。不过,您可以通过['strpos'](http://us2.php.net/manual/en/function.strrpos.php)函数获得创意。 – Jon 2013-04-24 08:04:12

+0

匹配这个正则表达式:'/(%[^%] +%)/ g' – elclanrs 2013-04-24 08:04:36

+0

你必须找到第一次发生然后替换这个,然后找到第二次发生然后替换这个 – 2013-04-24 08:04:40

回答

3

使用正则表达式(PHP 5.3+需要)

$string = 'Bla bla %yada yada% bla bla %yada yada%'; 
echo preg_replace('/%([^%]*)%/', '<a href="http://example.com">$1</a>', $string, 1) . '<br>'; // to replace the first instance. 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // 
$links = array('http://example.com', 'http://stackoverflow.com', 'http://google.com'); 
$index = 0; 
echo preg_replace_callback('/%([^%]*)%/', function($m) use($links, &$index){ 
    $m[1] = '<a href="'.$links[$index].'">'.$m[1].'</a>'; 
    $index++; 
    // reset the index if it exceeds (N links - 1) 
    if($index >= count($links)){ 
     $index = 0; 
    } 
    return $m[1]; 
}, $string).'<br>'; // to replace according to your array 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // 
// To test with a string that contains more %% than the links 
$string2 = 'Bla bla %yada yada% bla bla %yada yada% wuuut dsfsf %yada yada% sjnfsf %yada yada% jnsfds'; 
$links = array('http://example.com', 'http://stackoverflow.com', 'http://google.com'); 
$index = 0; 
echo preg_replace_callback('/%([^%]*)%/', function($m) use($links, &$index){ 
    $m[1] = '<a href="'.$links[$index].'">'.$m[1].'</a>'; 
    $index++; 
    // reset the index if it exceeds (N links - 1) 
    if($index >= count($links)){ 
     $index = 0; 
    } 
    return $m[1]; 
}, $string2).'<br>'; // to replace according to your array 

Online demo

+1

有效,但在某些(较旧的)服务器上,您将收到T_FUNCTION错误。为了解决这个问题,你需要命名你的函数,然后像这样调用它:'preg_replace_callback('/%([^%] *)%/','my_function',$ string)'。例如:http://codepad.org/zUMXvbEN – 2013-04-24 08:23:05

+0

@Jules我知道,这是假设你正在使用** PHP 5.3 + ** – HamZa 2013-04-24 08:23:41

+0

以这种方式我不会知道%%?因为这是我的问题......因为许多不同的语言中有很多字符串,所以我不能混淆实际的文本。我所要做的就是让它可以连接。 – Kapn0batai 2013-04-24 08:25:44

0

试试这个
支持PHP 4和PHP 5


解决方案:

$string ='Bla bla %yada yada% bla bla %yada yada%'; 

// Count no of % 
$count = substr_count($string,'%'); 

// Valid string pattern 
if (0 == ($count % 2)) { 

    $urlString = $string; 

    // Iterate for each pair of % to make it link 
    for ($i=1; $i <= $count/2 ; $i++ ) { 

     $urlString = preg_replace('/%/', "<a href='link$i'>", $urlString, 1); 
     $urlString = preg_replace('/%/', "</a>", $urlString, 1); 
    } 
} 
// Invalid string pattern 
else { 
    echo "Invalid string pattern"; 
} 

// Display generated link 
echo $urlString; 
preg_replace函数的功能

工作

  • 取下前两%
$str ='Bla bla %yada yada% bla bla %yada yada%'; 
    $newStr = preg_replace('/%/', '', $str, 2); 

    echo $newStr; 

    // Output => Bla bla yada yada bla bla %yada yada% 

  • 删除最后两个%
$str ='Bla bla %yada yada% bla bla %yada yada%'; 
    $newStr = preg_replace('/%/', '', strrev($str), 2); 
    $newStr = strrev($newStr); 

    echo $newStr; 

    // Output => Bla bla %yada yada% bla bla yada yada 

  • 移除所有%
$str ='Bla bla %yada yada% bla bla %yada yada%'; 
    $newStr = preg_replace('/%/', '', $str); 

    echo $newStr; 

    // Output => Bla bla yada yada bla bla yada yada 

参考
http://in1.php.net/preg%5Freplace

+0

没有模式,它不允许打开或关闭'a'标签。 – Jon 2013-04-24 08:05:13

+0

-1;缺少'=',pcre表达无效 – Vyktor 2013-04-24 08:05:33

+0

如果只有一个链接,应该是一个评论。 – Jon 2013-04-24 08:10:09