亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法根據上面的文本行轉換字符串?

有沒有辦法根據上面的文本行轉換字符串?

PHP
哈士奇WWW 2022-12-03 11:04:03
我有一個很長的數組列表,其中包括屬行(以大寫字母開頭,如:ACHNANTHES)和物種行(以大寫字母開頭,然后是一個點,如:A.)我需要根據上面的文字轉換一小部分。在下面你可以很容易地理解:ACHNANTHESA. brevipesA. coarctataA. cocconeiformisA. gibberulaA. lacunarum A. lineariformisA. longipesA. nolliiA. parvulaA. peterseniiA. pyrenaicumA. stolidaA. thermalisA. trinodisA. wellsiaePLATESSAP. conspicuaP. montanaP. salinarumACHNANTHIDIUMA. affineA. deflexumA. exiguumA. exileA. lanceolatumA. minutissimumA. minutumA. thermale我想將其轉換為:ACHNANTHESAchantes brevipesAchantes coarctataAchantes cocconeiformisAchantes gibberulaAchantes lacunarumAchantes lineariformisAchantes longipesAchantes nolliiAchantes parvulaAchantes peterseniiAchantes pyrenaicumAchantes stolidaAchantes thermalisAchantes trinodisAchantes wellsiaePLATESSAPlatessa conspicuaPlatessa montanaPlatessa salinarumACHNANTHIDIUMAchanthidium affineAchanthidium deflexumAchanthidium exiguumAchanthidium exileAchanthidium lanceolatum我想我需要在 PHP 中使用 while 或 foreach,但我不知道該怎么做。請幫忙。
查看完整描述

3 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

我有點失望,因為我沒有足夠的詳細信息讓您一路進入查詢過程,所以我只會改變您的元素值。

  1. 建立分組字符串——Genus 變量。在進入循環之前將其設置為 null

  2. 迭代時,通過提取第一個詞確定當前行是否為屬值,然后檢查它是否僅由大寫字母組成。

    • 如果是,將其緩存為新的分組值并將其存儲到輸出數組

    • 如果不是,則將格式化的“屬種”字符串推入結果數組

我喜歡正則表達式,但由于您的數據已經拆分為元素,因此使用正則表達式執行此任務沒有任何好處。

代碼:(演示

$result = [];

$currentGenus = null;

foreach ($array as $line) {

    $firstWord = strstr($line, ' ', true);

    if (ctype_upper($firstWord)) {

        $currentGenus = $firstWord;

        $result[] = $firstWord;

    } else {

        $result[] = ucfirst(strtolower($currentGenus)) . ' ' . explode(' ', $line, 3)[1];

    }

}

var_export($result);

輸出:


array (

  0 => 'ACHNANTHES',

  1 => 'Achnanthes brevipes',

  2 => 'Achnanthes coarctata',

  3 => 'Achnanthes cocconeiformis',

  4 => 'Achnanthes gibberula',

  5 => 'Achnanthes lacunarum',

  6 => 'Achnanthes lineariformis',

  7 => 'Achnanthes longipes',

  8 => 'Achnanthes nollii',

  9 => 'Achnanthes parvula',

  10 => 'Achnanthes petersenii',

  11 => 'Achnanthes pyrenaicum',

  12 => 'Achnanthes stolida',

  13 => 'Achnanthes thermalis',

  14 => 'Achnanthes trinodis',

  15 => 'Achnanthes wellsiae',

  16 => 'PLATESSA',

  17 => 'Platessa conspicua',

  18 => 'Platessa montana',

  19 => 'Platessa salinarum',

  20 => 'ACHNANTHIDIUM',

  21 => 'Achnanthidium affine',

  22 => 'Achnanthidium deflexum',

  23 => 'Achnanthidium exiguum',

  24 => 'Achnanthidium exile',

  25 => 'Achnanthidium lanceolatum',

  26 => 'Achnanthidium minutissimum',

  27 => 'Achnanthidium minutum',

  28 => 'Achnanthidium thermale',

  29 => 'EUCOCCONEIS',

  30 => 'Eucocconeis flexella',

  31 => 'Eucocconeis laevis',

  32 => 'Eucocconeis quadratarea',

)


查看完整回答
反對 回復 2022-12-03
?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

我很高興被證明是錯誤的,但我認為使用簡單替換的 PCRE 正則表達式引擎無法獲得所需的結果。


假設字符串是


ACHNANTHES

A. brevipes

A. coarctata

A. cocconeiformis

PLATESSA

P. conspicua

P. montana

P. salinarum

如果你顛倒線條以獲得


P. salinarum

P. montana

P. conspicua

PLATESSA

A. cocconeiformis

A. coarctata

A. brevipes

ACHNANTHES

你可以使用正則表達式


^[A-Z]\.(?=\s+[a-z]+\s*(?:[A-Z]\.\s+[a-z]+\s*)*([A-Z]+)\s*$)

獲取匹配項并將每個匹配項替換為捕獲組的內容,以獲得


PLATESSA salinarum

PLATESSA montana

PLATESSA conspicua

PLATESSA

ACHNANTHES cocconeiformis

ACHNANTHES coarctata

ACHNANTHES brevipes

ACHNANTHES

此時通過反轉這些行獲得所需的結果:


ACHNANTHES

ACHNANTHES brevipes

ACHNANTHES coarctata

ACHNANTHES cocconeiformis

PLATESSA

PLATESSA conspicua

PLATESSA montana

PLATESSA salinarum

演示


以下操作由 PHP 的正則表達式引擎 PCRE 執行。


^                # match beginning of line

[A-Z]\.          # match uc ltr then '.' 

(?=              # begin non-cap grp

  \s+[a-z]+\s*   # match 1+ whtspaces, 1+ lc ltrs, 0+ whtspaces

  (?:            # begin non-cap grp

    [A-Z]\.      # match line begin with uc ltr then '.'

    \s+[a-z]+\s* # match 1+ whtspaces, 1+ lc ltrs, 0+ whtspaces 

  )              # end non-cap grp

  *              # execute non-cap grp 0+ times

  ([A-Z]+)       # match 1+ uc ltrs in cap grp 1

  \s*            # match 0+ whtspaces

  $              # match end of line

)                # end positive lookahead      


查看完整回答
反對 回復 2022-12-03
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

為了解決您的問題,我會先考慮您將使用哪種邏輯,然后再考慮任何 PHP 語言細節。大多數通用編程語言(例如 PHP)可以完成您在字符串操作方面所需的大部分工作,因此現在不必擔心您將如何實現您的邏輯。

我認為在這種情況下使用正則表達式庫會有點矯枉過正。有很多方法可以解決您的問題,而且通常有一個比我首先想到的更好的方法,但我將復習一下我首先想到的邏輯。

首先,我將回顧一些重要的假設。這意味著屬行僅包含字母,而種行將以字母開頭,然后是點。我還假設了三個新事物:

  1. 除了屬行和種行之外,沒有其他類型的行

  2. 屬行至少有兩個字符長

  3. 第一行是屬名。

所有這些假設都應該是真實的,如果它們是真實的,那么這個解決方案就會起作用。這是我的英語邏輯:

Declare a variable that will be a string that keeps track of your current genus name 


For each line (AKA for each string in your array), do this chunk of code:

  See if the second letter of the current line is not a dot

    If it is not, this line is your current genus name: change

      your current genus name variable to the current line

  BUT... if the second letter of the current line IS a dot

    This is a species line, and we will need to transform it, and to do that...

    Make a new string that is the current line with the first two characters cut off

    Make a new string copy of your current genus name, but where it just 

      starts with a capital instead of being all-caps

    Make a new string, which is those two strings you just made put together

    Replace the current line with that newest string you just made

現在,我不會給你一個徹底的解決方案,因為如果我剝奪你這個學習機會,Stack Overflow 會恨我,但我會讓你知道一些有用的語法來解決這個問題。

foreach 循環 https://www.w3schools.in/php/looping/foreach/

字符串 https://www.php.net/language.types.string(搜索“按字符訪問和修改字符串”)

if 和 else 語句 https://www.w3schools.com/php/php_if_else.asp

子串 https://www.php.net/manual/en/function.substr.php

有用的字符串大小寫函數 https://www.javatpoint.com/php-string-strtolower-function

字符串連接 https://www.php.net/manual/en/language.operators.string.php

PS - 一個真正好的解決方案將有錯誤處理,比如如果屬名只有一個字符長,或者只有返回字符的行,等等,但是為了簡單起見,我沒有'在此解決方案中不要這樣做。這個答案應該適合您的目的,請記住,錯誤處理是一種很好的做法,并且會為您省去很多麻煩。


查看完整回答
反對 回復 2022-12-03
  • 3 回答
  • 0 關注
  • 152 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號