首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用explode函数来找出这样的结果?并且请解释一下在分解函数中使用的每个$delimiter的含义

如何使用explode函数来找出这样的结果?并且请解释一下在分解函数中使用的每个$delimiter的含义
EN

Stack Overflow用户
提问于 2017-07-06 14:53:14
回答 3查看 87关注 0票数 0

我的输入是$text = '( ( LED AND DIODE ) OR ( "LEE power" and system ) ) '

我正在对此输入应用分解函数

代码语言:javascript
复制
function multiexplode ($delimiters,$string) 
{
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}               
$exploded = multiexplode(array(' ',":" ), $text);
for($i=0;$i<(count($exploded));$i++)
    echo "<br> $exploded[$i]";

我的输出如下所示

代码语言:javascript
复制
0 - (   
1 - (  
2 - LED  
3 - AND  
4 - DIODE  
5 - )  
6 - OR  
7 - (  
8 - "LEE  
9 - power"  
10 - and  
11 - system  
12 - )  
13 - )  

但我希望输出如下所示

代码语言:javascript
复制
0 - (  
1 - (  
2 - LED  
3 - AND  
4 - DIODE  
5 - )  
6 - OR  
7 - (  
8 - LEE power  
9 - and  
10 - system  
11 - )  
12 - )  

我希望将“”中的所有单词存储在数组的一个元素中。

EN

回答 3

Stack Overflow用户

发布于 2017-07-06 15:31:17

检查以下代码:

代码语言:javascript
复制
$text = '( ( LED AND DIODE ) OR ( "LEE power" and system ) ) ';

preg_match_all('/"(?:\.|[^"])*"|\S+/', $text, $exploded);
for($i=0;$i<(count($exploded[0]));$i++)
    echo "<br>". $exploded[0][$i];

基于comment的更新

对于$text = '((LED AND DIODE) OR ("LEE power" and system)) ';的输入

代码语言:javascript
复制
$text = '((LED AND DIODE) OR ("LEE power" and system)) ';
$re = '/"(?:\.|[^"])*"|\(?|\)|(\'(.*?)\'|(\w+))/';
preg_match_all($re, $text, $exploded);

for($i=0;$i<(count($exploded[0]));$i++)
    if($exploded[0][$i]!='')
        echo "<br>". $exploded[0][$i];
票数 2
EN

Stack Overflow用户

发布于 2017-07-06 16:07:21

虽然这不是一个经过全面测试的答案,但它回答了您所描述的当前问题。祝你好运!

代码语言:javascript
复制
function concatenateQuotedElements($input_array)
{
  //As per the question, we'll be exploding our array by spaces " ".
  $pieces = explode(" ", $input_array);

  //This array will be returned as our final array of concatenated elements.
  $output_array = array();


  /*When we happen upon an element containing a 
  parenthesis ('"') as the first character, we'll 
  temporarily store so that we may concatenate 
  future elements with it.*/
  $stored_element = null;
  $stored_position = null; 

  /*Parse through our array and look for any character 
  that contains a " and see whether it's at the beginning 
  or the end of the string element.*/
  for ($i = 0; $i < count($pieces); $i++)
  {   

    //If we detect a parenthesis in our element...  
    if (strpos($pieces[$i], '"') !== false)
    { 

      //See if it's at the beginning, and if it is, store it or future use.      
      if (substr($pieces[$i], 0, 1) === '"')
      {
        $stored_element = $pieces[$i];
        $stored_position = $i;
      }

      /*Or, see if it's at the end of the string. If it is, we need to see 
       if there's been a previous element with an opening parenthesis that 
       we've stored and concatenate this element onto our stored element.*/
      if (
          substr($pieces[$i], -1) === '"' && 
          $stored_element !== null && 
          $stored_position !== null
         )
      {
        $output_array[$stored_position] = $stored_element . " " . $pieces[$i];

        /*Reset our temporary storage so that it hold 
        any forthcoming elements with parenthesis.*/  
        $stored_element = null;
        $stored_position = null;

        /*Finally, [Continue] to the next element without 
        pushing this as a non-concatenated, standalone element.*/
        continue;
      }
    }

    array_push($output_array, $pieces[$i]);
  }

  return $output_array;
}

对于与给定数组相关的特定输出,您可以使用:

代码语言:javascript
复制
//Our example array given in the question:
$array = '( ( LED AND DIODE ) OR ( "LEE power" and system ) )';

//Our output:
$concatenate_quotes_array = concatenateQuotedElements($array);

//"Pretty print" the array result of the function
echo "<pre>";
print_r($concatenate_quotes_array);
echo "</pre>";

输出:

代码语言:javascript
复制
Array
(
    [0] => (
    [1] => (
    [2] => LED
    [3] => AND
    [4] => DIODE
    [5] => )
    [6] => OR
    [7] => (
    [8] => "LEE power"
    [9] => and
    [10] => system
    [11] => )
    [12] => )
)
票数 1
EN

Stack Overflow用户

发布于 2017-07-06 15:05:03

这里

代码语言:javascript
复制
<?php
    $text = str_replace('LEE power','LEE-power',"( ( LED AND DIODE ) OR ( 'LEE power' and system ) ) ");
    $newArray = explode(' ',$text);
    foreach($newArray as $row) $data[] = str_replace('-',' ',$row);
    echo "<pre>";print_r($data);die;
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44941719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档