English 中文(简体)
在PHP中为多位分离者铺设护栏
原标题:Splitting a string on multiple separators in PHP

我可以用<代码>preg_split将体形同 com分开。

$words = preg_split( /[,]/ , $string);

How can I use a dot, a space and a semicolon to split string with any of these?

PS。 我找不到PHP前页的任何相关例子,这说明我为什么要问。

最佳回答

为此:

<?php

$string = "foo bar baz; boo, bat";

$words = preg_split( /[,.s;]+/ , $string);

var_dump($words);
// -> ["foo", "bar", "baz", "boo", "bat"]

解释<>

<代码>[为特性类别,特性类别包括多重特性,与类别内特性之一对应

<代码>。 特征是,不需要从inside品级中逃脱。 虽然在非属性类别的情况下,需要避免这种情况,因为<代码>.是指“具有任何特性”。

<代码>s匹配白色空间

<代码>;,在半殖民地上分裂,这不必逃脱,因为它没有特别意义。

<代码>+最终确保分离后的空间不显示为匹配。

问题回答

The examples are there, not literally perhaps, but a split with multiple options for delimiter

$words = preg_split( /[ ;.,]/ , $string);

类似情况?

<?php
$string = "blsdk.bldf,las;kbdl aksm,alskbdklasd";
$words = preg_split( /[, .;]/ , $string);

print_r( $words );

result:

Array
(
    [0] => blsdk
    [1] => bldf
    [2] => las
    [3] => kbdl
    [4] => aksm
    [5] => alskbdklasd
)
$words = preg_split( /[,. ]/ , $string);

仅添加这些果实。

$words = preg_split( /[;,. ]/ , $string);

www.un.org/Depts/DGACM/index_spanish.htm 由于Igoris Azanovas,不需要具有品格的 escaping;

$words = preg_split( /[,.s;]/ , $string);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签