English 中文(简体)
PHP 预出匹配混淆
原标题:PHP preg_match confusion
  • 时间:2012-05-23 21:25:40
  •  标签:
  • php
  • regex

我有一条像:

$string =  "lorem ipsum","dolor, sit","amet" ;

我试图利用ExplaDE和预配,以便将每个元素都纳入 ". " 。 " 将原始字符串变成一个阵列。

最终会给我一个机会

$array[0] =  lorem ipsum ;
$array[1] =  dolor, sit ;
$array[2] =  amet ;

某字符串将含有介于两者之间的逗号,所以我试图爆炸以将其分离,然后替换或预加配方以提取介于 ". " 之间。

我如何能够实现所期望的目标?

谢谢

最佳回答
问题回答

此代码应该反复将您的字符串解析成其组件 :

$array = array();
while (preg_match( /^"([^"]*)"(?:,(.*)|$)/ , $string, $match)) {
    $array[] = $match[1];
    $string = @$match[2];
}

您正在寻找 preg_split :

$string = preg_split("/,[^s+]/",  "lorem ipsum","dolor, sit","amet" );
print_r($string);

& gt; 的 & gt;

Array
(
    [0] =& gt; 的 & gt; "lorem ipsum"
    [1] =& gt; 的 & gt; dolor, sit"
    [2] =& gt; 的 & gt; amet"
)




相关问题
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 ...

热门标签