2013-12-18 28 views
0

我确实遇到了问题,在我的情况下,我选择了 将MYSQL数据库中的信息保存到$ data中。preg_replace用于替换变量的所有内容

和我有2个数组preg_replace。

这是一个例子:

$repl(); 
$repl[0] = '/bull/'; 
$repl[1] = '/found/'; 
$repl[2] = '/search/'; 

$replto(); 
$replto[0] = 'This is not a ballon'; 
$replto[1] = 'This has been found'; 
$replto[2] = 'Im looking for it'; 

$data = array(); 
$data[0] = '/mynickname/search'; 
$data[1] = '/somebulls/search'; 
$data[2] = '/mcdo/found'; 
$data[3] = '/bump/search'; 
$data[4] = '/blood/bull'; 

echo preg_replace($repl,$replto,$data); 

好,但preg_replace函数的输出是这样的:

/mynickname/Im looking for it 
/somebulls/Im looking for it 
/mcdo/This has been found 
/bump/Im looking for it 
/blood/This is not a ballon 

...但我想这样的输出:

Im looking for it 
Im looking for it 
This has been found 
Im looking for it 
This is not a ballon 

我'新的PHP,我解决了很多问题,但这是一个问题,我还没有找到解决方案。

你能帮助我吗?

+1

'strrpos'(http://php.net/strrpos)和'substr'(http://php.net/substr)的组合应该很容易做到。 –

+0

那么,代码的工作原理与您的一样...我认为您的逻辑在这里不正确。如果将'search'替换为'Im正在寻找它',为什么你会认为'/ mynickname/search'会自动删除mynickname部分?它只是取代你告诉它的那部分。 – Charlie74

+0

哇...很容易?...不适合我,我认为这只是addnal在“preg_replace”中的一个事情......就像在SQL中使用LIKE中的%一样。 – user3114471

回答

0

根据您想要的结果,它看起来像你想要做的事,如:

$repl(); 
$repl[0] = '/.*bull/'; 
$repl[1] = '/.*found/'; 
$repl[2] = '/.*search/'; 
+0

/*将为0或更多次出现/。这不是你想要的。最重要的是,/正被用作模式分隔符,所以*真的不知道要应用什么。 –

+0

正确;编辑... –

+0

非常感谢,它完美的工作。不能上去投票答案:/我没有声望。现在我能 :) – user3114471

1

十分感谢它完美地工作。

让像

$repl(); 
$repl[0] = '/.*bull/'; 
$repl[1] = '/.*found/'; 
$repl[2] = '/.*search/'; 

对不起,我迟到回答,不能提前到来。

非常感谢。