PHPで区切り文字で文字列を分割する方法は?
使用explode()函数可按分隔符拆分字符串,其语法为explode(分隔符, 字符串, 限制数),例如explode(",", "apple,banana")返回数组['apple','banana'];限制参数可控制返回元素数量,如explode("-", "one-two-three", 2)得['one','two-three'];若需多分隔符支持,则应用preg_split()配合正则表达式,如preg_split('/[,;\s]+/', "apple;banana,orange")实现更灵活拆分。
To split a string by a delimiter in PHP, use the explode() function. It breaks a string into an array based on a specified delimiter.
Basic Syntax of explode()
explode( string $delimiter, string $string, int $limit = PHP_INT_MAX )This function takes three parameters:
- delimiter – the character or string used to split the input
- string – the input string to split
- limit – optional; controls the number of array elements returned
Simple Example
Split a comma-separated string:
$fruits = "apple,banana,orange";$fruitArray = explode(",", $fruits);
print_r($fruitArray);
Output:
Array(
[0] => apple
[1] => banana
[2] => orange
)
Using a Limit
You can limit the number of resulting elements:
$text = "one-two-three-four";$parts = explode("-", $text, 3);
print_r($parts);
Output:
Array(
[0] => one
[1] => two
[2] => three-four
)
With a limit of 3, the third element contains the remainder of the string.
Splitting with Multiple Characters
If you need to split by multiple delimiters (e.g., comma, semicolon, space), consider using preg_split() with a regular expression:
$input = "apple;banana,orange cherry";$items = preg_split('/[,;\s]+/', $input);
print_r($items);
This splits on commas, semicolons, or whitespace.
Basically, use explode() for simple delimiters. For complex splitting logic, switch to preg_split(). Both are reliable and widely used in PHP applications.
以上がPHPで区切り文字で文字列を分割する方法は?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undress AI Tool
脱衣画像を無料で

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Stock Market GPT
AIを活用した投資調査により賢明な意思決定を実現

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック

preventXsssssbyescapingOutputwithhtmlspecialChars()orjson_encode()、validatinginputusingfilter_var()、applyingcspheaders、andusingsingsecureframeworkslikelaravel。

$ _POST HyperGlobal Arrayを使用してPOSTデータを取得し、フォーム名属性を使用して値を読み取り、配列入力を処理するときにForeachループを使用して、XSSを防ぐためにデータを検証およびフィルタリングする必要があります。

(配列)を使用して、単純なオブジェクトを配列に変換します。プライベートまたは保護されたプロパティが含まれている場合、キー名に特殊文字があります。ネストされたオブジェクトの場合、再帰関数を使用して変換を横断して、すべての階層オブジェクトが連想配列になるようにする必要があります。

PHPのGDライブラリを使用して、画像に透かしを追加します。最初に元の画像と透かし(テキストまたは画像)をロードし、次にimageCopy()またはImagetTftext()を使用してマージし、最後に出力を保存します。 JPEG、PNG、その他の形式をサポートし、透明性とフォントパスの処理に注意し、GD拡張機能が有効になっていることを確認してください。

intializecurlwithcurl_init()、setoptionslikeurl、method、andheaders、senddatausingpostorcustomtometods、handleresponseviacurl_exec()、checkerrorswithcurl_error()、retievestatususingcurl_getinfo()、decodejsonrespurlletosons(

Qushu.com Downbookの最新のアクセスアドレスはhttps://downbook.cc/です。このプラットフォームは、さまざまなトピックをカバーする豊富な電子書籍リソースを提供し、カスタマイズされた読書設定、ナイトモード、オフラインのダウンロード、デバイス間の同期の読み取りの進捗状況をサポートし、ユーザーがスムーズで快適な読書体験をしていることを保証します。

AISIアシスタントの公式ダウンロードポータルは、公式Webサイトhttps://www.i4.cn/にあり、コンピューターとモバイルのダウンロード、デバイス管理、アプリケーションのインストール、モードスイッチング、画面投影、ファイル管理機能をサポートしています。

exploit()関数を使用して文字列をセパレーターで分割し、その構文はexplore( "、"、 "、" apple、banana ")などのexplore(delimiter、string、lime number)です。 LIMITパラメーターは、Explore( " - "、 "One-tw-three"、2)などの返された要素の数を制御して['one'、 '2-three']を取得できます。複数のセパレータが必要な場合、preg_split()などのpreg_splitなどの正規表現でpreg_split()が使用されます
