我在我的数据库行中有这个数据,我想用42/05/47/6这个整数替换4205476整数。我可以使用preg匹配提取数字但是我又很困惑如何实现这一点 这是一行mysql,如果替换了图像之后的所有数字,它将如下所示(参见下一行)
Mario Ermito photos by sample.com Mario Ermito Latest News, Photos, Biography, Videos and Wallpapers [img]http://xyz.sample.com/image/4205476/600full-mario-ermito.jpg[/img][img]http://xyz.sample.com/image/4453948/600full-my-profile.jpg[/img][img]http://xyz.sample.com/image/427185/600full-eagle-eye-poster.jpg[/img][img]http://xyz.sample.com/image/1323868/600full-alexis-bledel.jpg[/img][img]http://xyz.sample.com/image/2505314/600full-monroe-lee.jpg[/img][img]http://xyz.sample.com/image/3300481/600full-cindy-crawford.jpg[/img][img]http://xyz.sample.com/image/1046646/600full-pitura-freska.jpg[/img][img]http://xyz.sample.com/image/4322305/600full-kristin-kreuk.jpg[/img][img]http://xyz.sample.com/image/4261476/600full-kang-so--ra.jpg[/img][img]http://xyz.sample.com/image/3386911/600full-summer-brielle.jpg[/img][img]http://xyz.sample.com/image/4663949/600full-the-closer-artwork.jpg[/img]
更换后它将如下所示
Mario Ermito photos by sample.com Mario Ermito Latest News, Photos, Biography, Videos and Wallpapers [img]http://xyz.sample.com/image/42/05/47/6/600full-mario-ermito.jpg[/img][img]http://xyz.sample.com/image/44/53/94/8/600full-my-profile.jpg[/img][img]http://xyz.sample.com/image/42/71/85/600full-eagle-eye-poster.jpg[/img][img]http://xyz.sample.com/image/13/23/86/8/600full-alexis-bledel.jpg[/img][img]http://xyz.sample.com/image/25/05/31/4/600full-monroe-lee.jpg[/img][img]http://xyz.sample.com/image/33/00/48/1/600full-cindy-crawford.jpg[/img][img]http://xyz.sample.com/image/10/46/64/6/600full-pitura-freska.jpg[/img][img]http://xyz.sample.com/image/43/22/30/5/600full-kristin-kreuk.jpg[/img][img]http://xyz.sample.com/image/42/61/47/6/600full-kang-so--ra.jpg[/img][img]http://xyz.sample.com/image/33/86/91/1/600full-summer-brielle.jpg[/img][img]http://xyz.sample.com/image/46/63/94/9/600full-the-closer-artwork.jpg[/img]
我试图从这个输入中提取数字,我可以用这个模式做到这一点,但下一步是什么?非常困惑。首先想到添加字符串但无法决定如何实现
~\/\image\/(.*?)\/~
答案 0 :(得分:0)
(?<=image/)(\d{2})(\d{2})(\d{2})(\d)(?=/)
这将捕获image/
和/
之间的2位,2位,2位和1位数字。然后,您可以使用preg_replace()
在每组数字之间用/
替换字符串。
$string = 'http://xyz.sample.com/image/4205476/600full-mario-ermito.jpg';
$regex = '~(?<=image/)(\d{2})(\d{2})(\d{2})(\d)(?=/)~';
$string = preg_replace($regex, '$1/$2/$3/$4', $string);
echo $string; // http://xyz.sample.com/image/42/05/47/6/600full-mario-ermito.jpg
工作示例:ideone
<强>解释强>
正则表达式以(<?=image/)
开头,&#34;看后面&#34;对于字符串image/
。然后我们使用捕获组(在括号中,以便我们可以使用$1
,$2
等访问)\d
(这是&#34;简写&#34;字符类{ {1}})。我们在花括号上指定每个匹配组中的位数,因此我们捕获2位数,2位数,2位数,然后是一位数。最后,正则表达式以[0-9]
结束,&#34;向前看&#34;对于字符串(?=/)
。我们现在正式将字符串/
与已捕获的image/1234567/
,12
,34
,56
组进行匹配。
我们使用PHP的正则表达式函数7
来匹配您的字符串,并将其替换为我们的捕获组(在每个字符串之间添加preg_replace()
)。
/
答案 1 :(得分:0)
您可以使用wordwrap
加上array_walk
<?php
$str=' Mario Ermito photos by sample.com Mario Ermito Latest News, Photos, Biography, Videos and Wallpapers [img]http://xyz.sample.com/image/4205476/600full-mario-ermito.jpg[/img][img]http://xyz.sample.com/image/4453948/600full-my-profile.jpg[/img][img]http://xyz.sample.com/image/427185/600full-eagle-eye-poster.jpg[/img][img]http://xyz.sample.com/image/1323868/600full-alexis-bledel.jpg[/img][img]http://xyz.sample.com/image/2505314/600full-monroe-lee.jpg[/img][img]http://xyz.sample.com/image/3300481/600full-cindy-crawford.jpg[/img][img]http://xyz.sample.com/image/1046646/600full-pitura-freska.jpg[/img][img]http://xyz.sample.com/image/4322305/600full-kristin-kreuk.jpg[/img][img]http://xyz.sample.com/image/4261476/600full-kang-so--ra.jpg[/img][img]http://xyz.sample.com/image/3386911/600full-summer-brielle.jpg[/img][img]http://xyz.sample.com/image/4663949/600full-the-closer-artwork.jpg[/img]';
preg_match_all('~\/\image\/(.*?)\/~', $str, $matches); echo "<pre>";
$matchesarray = $matches[1];
array_walk($matches[1],function (&$v){ $v = wordwrap($v, 2, "/", true);});
$replacearray = $matches[1];
$str = str_replace($matchesarray,$replacearray,$str);
echo $str;
<强> OUTPUT :
强>
Mario Ermito照片来自sample.com Mario Ermito最新消息,照片,传记,视频和壁纸 [IMG] http://xyz.sample.com/image/42/05/47/6/600full-mario-ermito.jpg [/ IMG] [IMG] http://xyz.sample.com/image/44 /53/94/8/600full-my-profile.jpg[/img][img]http://xyz.sample.com/image/42/71/85/600full-eagle-eye-poster.jpg[/ IMG] [IMG] http://xyz.sample.com/image/13/23/86/8/600full-alexis-bledel.jpg [/ IMG] [IMG] http://xyz.sample.com/image /25/05/31/4/600full-monroe-lee.jpg[/img][img]http://xyz.sample.com/image/33/00/48/1/600full-cindy-crawford.jpg [/img][img]http://xyz.sample.com/image/10/46/64/6/600full-pitura-freska.jpg[/img][img]http://xyz.sample.com /image/43/22/30/5/600full-kristin-kreuk.jpg[/img][img]http://xyz.sample.com/image/42/61/47/6/600full-kang-so --ra.jpg [/ IMG] [IMG] http://xyz.sample.com/image/33/86/91/1/600full-summer-brielle.jpg [/ IMG] [IMG]的http:// xyz.sample.com/image/46/63/94/9/600full-the-closer-artwork.jpg[/img]
首先使用preg_match_all
并获取所有这些数字并将其保存在单独的数组中,现在使用array_walk
将字符串拆分为斜线,最后在原始字符串上执行简单的str_replace