Php 字符串替换多个值
我有一个字符串如下所示: Bla Bla%yada yada%Bla Bla%yada yada% 是否有办法仅替换前两个“%”(或最后两个),以便获得下一个输出:Php 字符串替换多个值,php,string,Php,String,我有一个字符串如下所示: Bla Bla%yada yada%Bla Bla%yada yada% 是否有办法仅替换前两个“%”(或最后两个),以便获得下一个输出: Bla bla <a href='link1'>yada yada</a> bla bla %yada yada% Bla Bla Bla Bla%yada yada% 如有必要,最后两个“%”也会输出: Bla bla <a href='link1'>yada yada</a>
Bla bla <a href='link1'>yada yada</a> bla bla %yada yada%
Bla Bla Bla Bla%yada yada%
如有必要,最后两个“%”也会输出:
Bla bla <a href='link1'>yada yada</a> bla bla <a href='link2'>yada yada</a>
Bla-Bla-Bla-Bla
我不知道如何区分前两个和后两个,因此,如果我愿意,我可以用链接替换前两个或后两个标记“%”。
我正在使用php。提前谢谢
问候试试这个支持PHP4和PHP5
解决方案:
$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;
- 删除最后两个%
- 删除所有%
参考文献
使用regex(需要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
$string='Bla Bla%yada yada%Bla Bla%yada yada%';
echo preg_replace('/%([^%]*)%/','$string,1)。'
';//替换第一个实例。
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
$links=数组('http://example.com', 'http://stackoverflow.com', 'http://google.com');
$index=0;
echo preg_replace_回调('/%([^%]*)%/',函数($m)use($links,&$index){
$m[1]='';
$index++;
//如果索引超过(N个链接-1),请重置索引
如果($index>=计数($links)){
$index=0;
}
返回$m[1];
},$string)。“
”;//根据您的阵列进行替换
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
//使用包含%%以上链接的字符串进行测试
$string2='Bla Bla%yada yada%Bla Bla%yada yada%WUUT dsfsf%yada yada%sjnfsf%yada yada%jnsfds';
$links=数组('http://example.com', 'http://stackoverflow.com', 'http://google.com');
$index=0;
echo preg_replace_回调('/%([^%]*)%/',函数($m)use($links,&$index){
$m[1]='';
$index++;
//如果索引超过(N个链接-1),请重置索引
如果($index>=计数($links)){
$index=0;
}
返回$m[1];
},$string2)。“
”;//根据您的阵列进行替换
.当链接标识符为
%
时,可能会以多种不同的方式导致许多错误。不过,你可以对它的函数进行创新。与此正则表达式匹配:/([^%]+%)/g
你必须先找到第一个实例,然后替换它,然后再找到第二个实例,然后替换它this@Jon我不一定要用“%”。我可以用我想要的任何方式标记它。然后你应该想出一些你要坚持的更独特的东西,你可以从中创建一个好的正则表达式。BB代码是一个很好的例子,说明了如何以这种方式格式化某些内容并允许表达式替换^^没有模式,也不允许打开或关闭a
标记。如果只是一个链接,则应该是一个注释。可以工作,但在某些(较旧的)服务器上,您将收到t_函数错误。要解决此问题,您需要命名函数,然后像这样调用它:preg_replace_callback('/%([^%]*)%/','my_function',$string)
。示例:@Jules我知道,这是假设您以这种方式使用PHP5.3+,我不需要知道%%之间的文本吗?因为那是我的问题。。。我无法处理实际文本,因为有许多不同语言的字符串。我所要做的就是使其可链接。@Kapn0batai您不必知道%%
之间的内容,但如果它包含%%
,则会产生不需要的结果@Kapn0batai如果你不确定在%%
之间是否存在%%
,那么你可能应该像Jon说的那样采用BB码方法。话虽如此,我有点(接近)
$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
$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