2012-02-16 30 views
0

我想在数组插入mysql表之前替换这个URL。 我有标题说明和链接阵列,链接有太多“垃圾”php preg_replace这个网址

这是什么var_dump($ item [“link”]);看起来像: [“link”] => string(88)“http://www.hello.com/junk/junk-ju-junkj-junk-jun-ju-junk,-JU-1234”

我想要取代这样的链接: [“link”] => string(88)“http://www.hello.com/te/tst?te=1234”

因此,搜索“http://www.hello.com”和“非常最后一个” - “...在1234” 之前,并将其替换为“/ te/tst?te =”

如何在获取之前将其替换在mysql表中插入?

预先感谢您为您的专业知识和时间;)

回答

0

你预计其他顶级域名比“COM”等?假设

$item['link'] = 'http://www.hello.com/junk/junk-ju-junkj-junk-jun-ju-junk,-JU-1234'; 

你可以做

$pattern = '/(^http:\/\/\w+\.\w+\.com).*?-(\d+)/'; 
$replacement = '$1/te/tst?te=$2'; 
$item['link'] = preg_replace($pattern, $replacement, $item['link']); 

你会得到

$item['link'] = 'http://www.hello.com/te/tst?te=1234'; 
+0

完美:)作品就像我想要的。再次感谢 ! – Ossi 2012-02-16 18:50:48