0

Currently I have a string which pulls a title of a post and includes the amount of mb the file is going to be so it returns a bunch of numbers.

Here's the content we normally pull on post (this is all saved into one string)

<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>

Because I'm sure someone is going to ask how I'm converting the value above into a string, the data is being stored in WordPress the_content and I am converting it to an array.

$string = get_the_content();
$links = explode("\n",$string);
$arrlength = count($links);

How can I make it so that it always removes the mb count? I can't explode periods, or dashes because sometimes tracks include those.

B. Desai
  • 16,414
  • 5
  • 26
  • 47
Placeholder
  • 689
  • 5
  • 14

3 Answers3

2

You could try this regex! #\s-\s\d*\.\d*\sMB# https://regex101.com/r/jYAUWu/1

Here it is in some PHP code:

<?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";
}

Which will give you the following:

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

Check it out here! https://3v4l.org/KsjrW If you manage to find a string that breaks it let me know!

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
2

To add to delboy1978uk answer (since I can't comment yet), are the file sizes always in MB? Any chance of KB? You can tweak the regex to filter that too with this line:

$regex = '#\s-\s\d*.\d*\s[K|M]B#';
Thomas
  • 51
  • 3
1

Here is a regex replace that will work with one long string, not an array.

$re = '/\s-\s\d+\.\d+\sMB/'; // example match " - 4.0 MB"
$str = '<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>';
$subst = '';

$result = preg_replace($re, $subst, $str);

echo $result;  

https://3v4l.org/1YEN9

Andreas
  • 23,610
  • 6
  • 30
  • 62