【问题标题】:Need to extract strings from main string using PHP需要使用 PHP 从主字符串中提取字符串
【发布时间】:2014-08-25 05:03:55
【问题描述】:

我的主字符串是这样的

<div class="large-12 columns">
text 1
</div>
<div class="large-12 columns">
text 2
</div>
<div class="large-12 columns">
text 3
</div>
<div class="large-12 columns">
text 4
</div>
<div class="large-12 columns">
text 5
</div>

这里我想提取标签&lt;div class="large-12 columns"&gt;&lt;/div&gt;之间的字符串

我已经有了提取两个字符串之间内容的功能,但它只获取第一个标签(文本 1)

在上面的主字符串中我给出了 5 个文本,但真正的字符串可能包含 1 到 30。

以前有人做过,因为我是 PHP 新手。

我用来提取字符串的代码。

function get_string_between($string, $start, $end){
$string = " ".$string;
 $ini = strpos($string,$start);
 if ($ini == 0) return "";
 $ini += strlen($start);     
 $len = strpos($string,$end,$ini) - $ini;
 return substr($string,$ini,$len);
}

【问题讨论】:

标签: php html extract


【解决方案1】:

试试这个:已编辑

 <?php
$str='<div class="large-12 columns">
text 1
</div>
<div class="large-12 columns">
text 2
</div>
<div class="large-12 columns">
text 3
</div>
<div class="large-12 columns">
text 4
</div>
<div class="large-12 columns">
text 5
</div>';
$str1=str_replace('<div class="large-12 columns">','',$str);
$arr=str_replace('</div>',',',$str1);
$arr1=explode(',',$arr);
$values=array();
foreach ($arr1 as $key=>$value){
    if(isset($value) && $value!=''){
    $values[] = $value;
    }
}
print_r($values);

【讨论】:

  • 这将 留在数组中并从索引 1 开始。修改以提供更清晰的数组以在 php 中使用。
  • @nwolybugis print_r 返回
    给你。
  • 查看源中的结果:'Array ( [0] => [1] => text 1
    [2] => text 2
    [3] => text 3
  • [4] => 文本 4
    【解决方案2】:

    PHP 版本...

    <?php 
    $str='<div class="large-12 columns">
    text 1
    </div>
    <div class="large-12 columns">
    text 2
    </div>
    <div class="large-12 columns">
    text 3
    </div>
    <div class="large-12 columns">
    text 4
    </div>
    <div class="large-12 columns">
    text 5
    </div>';
    $arr2 = str_replace('</div>', ',', $str);
    $arr = explode(',', $str);
    foreach ($arr as &$value){
        $value = strip_tags($value);
    }
    print_r($arr);
    ?>
    

    【讨论】:

      【解决方案3】:

      jQuery 很容易做到这一点。 .text() 函数提取存储在类“.large-12”的 div 中的信息,并将它们放入一个名为 info 的变量中。因为我不知道你要对它们做什么,所以我使用了相同的 .text() 函数将它们放在一个 div 中,id 为 extract_divs jsfiddle

      <html>
      <head>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      
      <script>
      $(document).ready(function(){
          var info = $(".large-12").text();
          $("#extracted_divs").text(info);
      });
      </script>
      </head>
      <body>
      
      <div class="large-12 columns">
      text 1
      </div>
      <div class="large-12 columns">
      text 2
      </div>
      <div class="large-12 columns">
      text 3
      </div>
      <div class="large-12 columns">
      text 4
      </div>
      <div class="large-12 columns">
      text 5
      </div>
      
      <div id="extracted_divs">
      </div>
      
      </body>
      </html>
      

      【讨论】:

      • 不幸的是,这个问题被标记为php,而不是jquery
      猜你喜欢
      • 2017-04-01
      • 2014-06-24
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多