2013-07-13 40 views
0

我有這種格式輸入:PHP正則表達式查找和文本和數字取代

文字數字1:12.3456°,文字數字2:78.9012°。

我想用PHP代替這個到這裏:

GPS:12.3456,78.9012: 文本數字1:12.3456°,文字數字2:78.9012°。

所以,再一次,在大的文本輸入:

唧唧歪歪,隨機文本,喇嘛喇嘛...... 文字數字1:12.3456°,文字數字2:78.9012° 而更多的文字...

此輸出需要:

Bla bla bla,random text,bla bla ... GPS:12.3456,78.9012: Text number1:12.3456°,text number2:78.9012°。 而更多的文字...

輸出需要附加在此之前我搜索: 」 GPS:12.3456,78.9012:

的2號也於所有線路不同:12.3456和78.9012 所有其他人。固定(空格,其它字符。)

如果你剛纔如何檢測,並得到這條線從大的文本: 「Text number1: 12.3456°, text number2: 78.9012°.」 也有助於 如果我有這條線,我可以找到數字並進行替換。 我將使用爆炸來檢測數字(找到數字之前和之後的空格)和str_replace以將輸入替換爲輸出。我不知道這是最好的方法,但我知道這些功能。

(對不起,文本格式不工作,因爲我想要的。我固定的輸入,輸出,改變了「」空間)

謝謝!

+0

確定使用的preg_match提取號碼。但那裏可以有其他號碼嗎?我們可以假設我們提取的數字總是以###。####格式存在嗎? – DevZer0

+0

數字格式:1-2個數字。 1-4號碼。 1.1,2.2.1,1.22,1.333,1.4444或者也可以是22.4444。我需要輸入數字,然後生成附加文本(「GPS:number1,number2:」),然後用添加的輸出替換輸入(在輸入文本行之前添加GPS)。 –

+0

Web.Guard和PédeLeão代碼的完美結合,都非常棒!謝謝。 –

回答

2
$text = 'Bla bla bla, random text, bla bla... Text,number1: 12.3456°, text,number2: 78.9012°. And more text... '; 

echo preg_replace('%([\w\s,]+:\s(\d+\.\d+)°,\s[\w\s,]+:\s(\d+\.\d+)°)%ui', ' GPS:$2,$3: $1', $text); 



//Output: Bla bla bla, random text, bla bla... GPS:12.3456,78.9012: Text,number1: 12.3456°, text,number2: 78.9012°. And more text... 
+0

這也適用於更改後的文本。大!你也是最棒的!謝謝! :) –

+0

是否「文本,編號」始終固定? –

+0

修正了,但沒有數字,只有2個不同的文字與外國字符,如éá,但它很好,工作!你很棒! :) –

1

這不是很漂亮,但我遲到了晚餐!

<? 
$text = 'Bla bla bla, random text, bla bla... 
Text,number1: 12.3456°, text,number2: 78.9012°. 
And more text...'; 

$lines = array(); 
foreach(explode("\r\n",$text) as $line){ 
    $match = array(); 
    preg_match_all('/\d{0,3}\.?\d{0,20}°/', $line, $result, PREG_PATTERN_ORDER); 
    for ($i = 0; $i < count($result[0]); $i++) { 
     $match[] = $result[0][$i]; 
    } 
    if(count($match)>0){ 
     $lines[] = 'GPS:'.str_replace('°','',implode(',',$match)); 
    } 
    $lines[] = $line; 

} 
echo implode('<br>',$lines); 
?> 

Bla bla bla, random text, bla bla... 
GPS:12.3456,78.9012 
Text,number1: 12.3456°, text,number2: 78.9012°. 
And more text... 
+0

它接近優秀,但我的輸出是這樣的:---- GPS:12.3456,78.9012 Bla bla bla,random text,bla bla ...文本,編號1:12.3456°,文本,編號2:78.9012°。還有更多文字 ----所以不要插入新的文字,文字,數字1的地方,只是所有文字的開始地方。 –

+0

所以這一切都在一條線上?這條單線上有多個GPS座標嗎? –

+0

是的,全部在1行,輸入行:「Bla bla bla,random text,bla bla ... Text,number1:12.3456°,text,number2:78.9012°。more text ...」並且需要輸出行:「Bla bla bla,random text,bla bla ... GPS:12.3456,78.9012:Text,number1:12.3456°,text,number2:78.9012°。and more text ...」你可以看到不同之處。 –

1
$text = 'Bla bla bla, random text, bla bla... 
Text number1: 12.3456°, text number2: 78.9012°. 
And more text'; 
$pattern = '#[a-zá-úàü\d ,]+:\s?([\d.]+)°[^:]+:\s?([\d.]+)°#i'; 
return preg_replace_callback($pattern, function($match) { 
    return sprintf("GPS:%s,%s:\n%s.", 
     $match[1], 
     $match[2], 
     $match[0] 
    ); 
}, $text); 
+0

This output:「Bla bla bla,random text,bla bla ... Text,number1:GPS:12.3456,78.9012:Text,number1:12.3456°,text,number2:78.9012°and more text」----需要「GPS:12.3456,78.9012:」BEFORE「Text,number1:」text。 –

+0

你是最快和最好的,但我很傷心,我犯了錯誤。不是這一行:「文本,編號1:12.3456°,文本,編號2:78.9012°」。這:「文本編號1:12.3456°,文本編號2:78.9012°」。不是「,」只是文字中的空格分隔符。我現在更新文本。對不起,這是我的錯。 –

+0

仍然不工作,但也是我的錯,因爲在輸入沒有數字(例如我添加),所以不是這個確切的輸出:「文本編號1:12.3456°,文本編號2:78.9012°」這:「文本abcdég:47.9667°,文字:19.8333°。「所以,在2號碼之前沒有數字,只是帶有口音的字母,如á,é字符,就像「文字abcdég:47.9667°,文字:19.8333°」。現在的輸出是這樣的:「Bla bla bla,random text,bla bla ... Textabcdé!! mxcmgps :: 47.9667,19.8333 !! g:47.9667°,Textáécde:19.8333°.. And more text」 –