目前我有一个字符串,它会提取帖子的标题,并包含该文件的 mb 数量,以便返回一堆数字。
这是我们通常在帖子上提取的内容(这全部保存为一个字符串)
<a href="http://example.com/a0j8nm5l3wo0" target=_blank>01 song name.mp3 - 2.4 MB</a>
<a href="http://example.com/ic1jfhhzzfth" target=_blank>02 song name.mp3 - 4.0 MB</a>
<a href="http://example.com/zo2nv6bzy6gd" target=_blank>03 song name.mp3 - 3.3 MB</a>
<a href="http://example.com/5fyony1m4j0w" target=_blank>04 song name.mp3 - 3.5 MB</a>
<a href="http://example.com/3imswet27clg" target=_blank>05 song name.mp3 - 1.9 MB</a>
<a href="http://example.com/ml65j3gpdggv" target=_blank>06 song name.mp3 - 3.4 MB</a>
<a href="http://example.com/hmuk8il0a04j" target=_blank>07 song name.mp3 - 4.4 MB</a>
<a href="http://example.com/qx81e67ystd3" target=_blank>08 song name.mp3 - 3.9 MB</a>
<a href="http://example.com/g6fo1s64vzkj" target=_blank>09 song name.mp3 - 3.9 MB</a>
<a href="http://example.com/xo3hw4xyx372" target=_blank>10 song name.mp3 - 3.6 MB</a>
<a href="http://example.com/ivcth22eqygd" target=_blank>11 song name.mp3 - 3.3 MB</a>
因为我确定有人会问我如何将上述值转换为字符串,所以数据存储在WordPress the_content
中,我将其转换为数组。
$string = get_the_content();
$links = explode("\n",$string);
$arrlength = count($links);
如何制作它以便始终删除 mb 计数?我不能爆发句号或破折号,因为有时候曲目包括那些。
答案 0 :(得分:2)
你可以试试这个正则表达式! #\s-\s\d*\.\d*\sMB#
https://regex101.com/r/jYAUWu/1
这是一些PHP代码:
<?php
$x = [
'01 song name.mp3 - 2.4 MB',
'02 song name.mp3 - 4.0 MB',
'03 song name.mp3 - 3.3 MB',
'04 song name.mp3 - 3.5 MB',
'05 song name.mp3 - 1.9 MB',
'06 song name.mp3 - 3.4 MB',
'07 song name.mp3 - 4.41 MB',
'08 song name.mp3 - 13.9 MB',
'09 song name.mp3 - 3.9 MB',
'10 song name.mp3 - 3.6 MB',
'11 song name.mp3 - 3.3 MB',
];
$regex = '#\s-\s\d*.\d*\sMB#';
foreach ($x as $y) {
echo preg_replace($regex, '', $y)."\n";
}
这将为您提供以下内容:
01 song name.mp3
02 song name.mp3
03 song name.mp3
04 song name.mp3
05 song name.mp3
06 song name.mp3
07 song name.mp3
08 song name.mp3
09 song name.mp3
10 song name.mp3
11 song name.mp3
在这里查看! https://3v4l.org/KsjrW如果您设法找到打破它的字符串,请告诉我!
答案 1 :(得分:2)
要添加到delboy1978uk答案(因为我还没有评论),文件大小是否总是以MB为单位? KB的任何机会?你可以通过这一行调整正则表达式来过滤它:
$regex = '#\s-\s\d*.\d*\s[K|M]B#';
答案 2 :(得分:1)