簡體   English   中英

在PHP / HTML文本輸入中,如何刪除不想提交到數據庫的某些部分?

[英]In a PHP/HTML text input, how do I strip certain parts that I don't want submitted to the database?

當人們在我的網站上編輯他們的帳戶時,他們會感到困惑,並認為他們必須輸入所有他們的Steam個人資料鏈接,例如http://steamcommunity.com/id/######文本字段為####### ,所以,如果它們弄亂了,我如何從輸入中刪除除#####部分之外的所有內容?

怎么樣:

$lastPart = array_pop(explode('/', $_SERVER['REQUEST_URI']));

這將檢索REQUEST_URI的最后一個元素。

str_replace ("http://steamcommunity.com/id/","",$input); 是一種方式。

一種更可靠的方法是:

preg_replace ("@^.*id/@","",$input);

$input = "http://steamcommunity.com/id/herpderp";
$output = '';

if( preg_match(':/:', $input) ) {
    $output = explode('/', $input);
    if( !empty($output[count($output)-1]) {
        $output = $output[count($output)-1];
    } else {
        // in the case of a trailing slash, use the ~second~ last field
        // ie: http://steamcommunity.com/id/herpderp/
        $output = $output[count($output)-2];
    }
} else {
    $output = $input;
}

echo "SteamID: " . $output;

或者簡單地:

if( preg_match('/[^a-zA-Z0-9]/', $input) ) {
    die('Disallowed characters in SteamID. Please enter only your SteamID, and not the full URL.');
}

我喜歡第二個代碼,因為它讓用戶自行處理自己的功能,而不必不斷添加代碼行來處理用戶似乎設計的新問題和創新問題。 他們一直在努力證明,每當某物被宣布為“防白痴”時,宇宙只會產生更好的白痴。

暫無
暫無

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

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