簡體   English   中英

從 PHP 中的文本文件中提取並顯示所有 10 位數字逗號分隔

[英]Extract and display all 10 digit numbers comma separated from a text file in PHP

我正在尋找一種從包含字母、數字和特殊字符的文本文件中提取所有 10 位數字逗號分隔的方法。

我現在能做的是使用這段代碼:

$texts  = "cache/en.txt";
$contents = file_get_contents($texts);
$showtexts = explode(',', $contents);
$showtextsclean = preg_replace('/\d{10}/', '', $showtexts);

foreach($showtextsclean as $final) {
    echo $final;
}

上面的代碼所做的是通過顯示除 10 位數字之外的所有內容來做相反的事情。 我真正想要的是提取所有 10 位數字並將逗號與示例分開:

{"left-right-kato":1566025480,"ladies-night-sys-bjerre":1563248008,"catch-fire-bj-rnskov":1563179104,"jeg-dr-mte-om-en-r-d-lavth-ngende-sol-rasmus-walter":1562991777,"45-fahrenheit-girl-the-remixes-drew-sycamore":"not found","bebe-zk":1469386794,"ahora-rozenberg":1566345117,"precious-leave-me-on-the-floor-liss":1560827606}

這樣我就可以像這樣回應它:

1566025480,1563248008,1563179104,1562991777,1469386794,1566345117,1560827606
<?php
$showtextsclean = '{"left-right-kato":1566025480,"ladies-night-sys-bjerre":1563248008,"catch-fire-bj-rnskov":1563179104,"jeg-dr-mte-om-en-r-d-lavth-ngende-sol-rasmus-walter":1562991777,"45-fahrenheit-girl-the-remixes-drew-sycamore":"not found","bebe-zk":1469386794,"ahora-rozenberg":1566345117,"precious-leave-me-on-the-floor-liss":1560827606}';

$digig10 = '/\b(\d{10})\b/';
preg_match_all($digig10,$showtextsclean,$match);
print_r($match[1]);
?>

它取代了一切不是大於或小於 10 位的數字

並從末尾修剪逗號並開始

給出這個結果:

Array
(
    [0] => 1566025480
    [1] => 1563248008
    [2] => 1563179104
    [3] => 1562991777
    [4] => 1469386794
    [5] => 1566345117
    [6] => 1560827606
)

兩種解決方案在線PHP外殼的速度比較。

這個preg_match_all 解決方案的速度

Real time: 0.014 s
User time: 0.006 s
Sys. time: 0.009 s
CPU share: 99.45 %
Exit code: 0

json 解決方案的氣泡速度解決方案幾乎相同:

Real time: 0.017 s
User time: 0.011 s
Sys. time: 0.006 s
CPU share: 98.71 %
Exit code: 0

sulutio foreachberend

Real time: 0.016 s
User time: 0.006 s
Sys. time: 0.008 s
CPU share: 87.91 %
Exit code: 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM