2017-09-20 49 views
0

目前我有一個字符串,它拉出一個帖子的標題,幷包括文件的數量mb將返回一堆數字。替換數字1的值 - 20

下面是我們通常拉帖子的內容(這是所有保存到一個字符串)

<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

也許你可以用空值 –

+0

替代甲基溴這將幫助: https://stackoverflow.com/questions/1445506/get-content-between-two-strings-php –

回答

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如果你設法找到一個打破它的字符串,讓我知道!

+0

美麗,給了我適應當前版本的一些麻煩,但現在它完美地工作,謝謝你delboy! – Placeholder

+1

沒問題!在regex101網站上玩耍,你很快就會成爲正則表達式的忍者! – delboy1978uk

+1

你的點沒有被轉義。這意味着此模式可以匹配其他字符串。看到這裏https://regex101.com/r/jYAUWu/2 – Andreas

2

爲了增加delboy1978uk答案(因爲我不能評論還),是文件大小總是以MB爲單位?任何KB的機會?你可以調整的正則表達式與該行篩選過:

$regex = '#\s-\s\d*.\d*\s[K|M]B#'; 
+0

不錯的建議:-) – delboy1978uk

+0

好的答案,謝謝! – Placeholder

1

這裏是一個正則表達式替換,將有一個很長的字符串,而不是一個數組工作。

$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