簡體   English   中英

從 PHP 中的字符串開頭和結尾刪除引號

[英]Remove quotes from start and end of string in PHP

我有這樣的字符串:

"my value1" => my value1
"my Value2" => my Value2
myvalue3 => myvalue3 

我需要去掉" (雙引號) 最后並開始,如果這些存在,但如果 String 中有這種字符,那么它應該留在那里。例如:

"my " value1" => my " value1

我如何在 PHP 中執行此操作 - 是否有此功能或我必須自己編寫代碼?

字面的答案是

trim($string,'"'); // double quotes
trim($string,'\'"'); // any combination of ' and "

它將從字符串中刪除所有前導和尾隨引號。

如果您需要嚴格刪除第一個和最后一個引號以防它們存在,那么它可能是這樣的正則表達式

preg_replace('~^"?(.*?)"?$~', '$1', $string); // double quotes
preg_replace('~^[\'"]?(.*?)[\'"]?$~', '$1', $string); // either ' or " whichever is found

如果僅在前導和尾隨引號嚴格配對的情況下才需要刪除,請使用Steve Chambers 的答案中的函數

但是,如果您的目標是從 CSV 文件中讀取值,則fgetcsv是唯一正確的選項。 它將處理所有邊緣情況,同時剝離價值外殼。

我有一個類似的需求,並編寫了一個函數來從字符串中刪除前導和尾隨單引號或雙引號:

/**
 * Remove the first and last quote from a quoted string of text
 *
 * @param mixed $text
 */
function stripQuotes($text) {
  return preg_replace('/^(\'(.*)\'|"(.*)")$/', '$2$3', $text);
} 

這將產生下面列出的輸出:

Input text         Output text
--------------------------------
No quotes       => No quotes
"Double quoted" => Double quoted
'Single quoted' => Single quoted
"One of each'   => "One of each'
"Multi""quotes" => Multi""quotes
'"'"@";'"*&^*'' => "'"@";'"*&^*'

正則表達式演示(顯示正在匹配/捕獲的內容): https : //regex101.com/r/3otW7H/1

如果匹配您提供的模式, trim將從開頭和結尾刪除字符的所有實例,因此:

$myValue => '"Hi"""""';
$myValue=trim($myValue, '"');

會變成:

$myValue => 'Hi'.

這是一種僅刪除匹配的第一個和最后一個字符的方法:

$output=stripslashes(trim($myValue));

// if the first char is a " then remove it
if(strpos($output,'"')===0)$output=substr($output,1,(strlen($output)-1));

// if the last char is a " then remove it
if(strripos($output,'"')===(strlen($output)-1))$output=substr($output,0,-1);

盡管這個線程早就應該被殺死,但我忍不住用我稱之為最簡單的答案來回應。 我注意到這個線程在 17 日重新出現,所以我對此並沒有那么糟糕。 :)

使用 Steve Chambers 提供的樣本;

echo preg_replace('/(^[\"\']|[\"\']$)/', '', $input);

輸出如下;

Input text         Output text
--------------------------------
No quotes       => No quotes
"Double quoted" => Double quoted
'Single quoted' => Single quoted
"One of each'   => One of each
"Multi""quotes" => Multi""quotes
'"'"@";'"*&^*'' => "'"@";'"*&^*'

這只會刪除第一個和最后一個引號,它不會重復刪除額外的內容,也不關心匹配的結尾。

這是一篇舊帖子,但為了迎合多字節字符串,至少有兩種可能的路線可以遵循。 我假設正在執行引號剝離,因為引號被視為程序/INI 變量,因此是“某物”或“某物”但不是“混合引號”。此外,匹配引號之間的任何內容都是完好無損。
路線 1 - 使用正則表達式

function sq_re($i) {
    return preg_replace( '#^(\'|")(.*)\1$#isu', '$2', $i );
}

這使用 \\1 來匹配與開頭匹配的相同類型的引號。 u 修飾符,使其支持 UTF8(好吧,不完全支持多字節)


路線 2 - 使用 mb_* 函數

function sq($i) {
    $f = mb_substr($i, 0, 1);
    $l = mb_substr($i, -1);
    if (($f == $l) && (($f == '"') || ($f == '\'')) ) $i = mb_substr($i, 1, mb_strlen( $i ) - 2);
    return $i;
}

你需要使用正則表達式,看看:-

http://php.net/manual/en/function.preg-replace.php

或者,在這種情況下,您可以使用 substr 檢查字符串的第一個字符和最后一個字符是否是引號,如果是,則截斷字符串。

http://php.net/manual/en/function.substr.php

正則表達式怎么樣

//$singleQuotedString="'Hello this 'someword' and \"somewrod\" stas's SO";
//$singleQuotedString="Hello this 'someword' and \"somewrod\" stas's SO'";
$singleQuotedString="'Hello this 'someword' and \"somewrod\" stas's SO'";

$quotesFreeString=preg_replace('/^\'?(.*?(?=\'?$))\'?$/','$1' ,$singleQuotedString);

輸出

Hello this 'someword' and "somewrod" stas's SO   

如果您更喜歡性能而不是清晰度,則可以這樣做:

// Remove double quotes at beginning and/or end of output
$len=strlen($output);
if($output[0]==='"') $iniidx=1; else $iniidx=0;
if($output[$len-1]==='"') $endidx=-1; else $endidx=$len-1;
if($iniidx==1 || $endidx==-1) $output=substr($output,$iniidx,$endidx);

注釋有助於清晰......在字符串上類似數組的用法中的括號是可能的,並且比等效方法需要更少的處理工作,太糟糕了沒有長度變量或最后一個字符索引

暫無
暫無

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

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