在定义的位置提取子字符串

时间:2015-01-17 08:07:40

标签: php string performance parsing

我有一个html字符串,我想将某个子字符串提取为纯文本,始终从定义的位置开始,我该如何做到最好?

html看起来像:

<table class="views-table cols-4"><thead><tr><th class="views-field views-field-line-item-title">
            Title          </th>
<th class="views-field views-field-commerce-unit-price">
            Unit price          </th>
<th class="views-field views-field-quantity">
            Quantity          </th>
<th class="views-field views-field-commerce-total views-align-right">
            Total          </th>
</tr></thead><tbody><tr class="odd views-row-first views-row-last"><td class="views-field views-field-line-item-title">
             "Sweet Heart" Package    (SPA02)          </td>
<td class="views-field views-field-commerce-unit-price">
            135.00  CAD          </td>
<td class="views-field views-field-quantity">
            1          </td>
<td class="views-field views-field-commerce-total views-align-right">
            135.00  CAD          </td>
</tr></tbody></table>

我想提取字符串"Sweet Heart" Package (SPA02),我该如何最有效地完成这项工作?

由于

编辑1 我采用了Vinie的解决方案进行旋转,只是在调用get_string()之前添加了回声,但由于某些原因,strpos()无法在$start内找到$string,这是奇怪,因为我把它们都打印到屏幕上,我可以看到,内容肯定在字符串中!

<?php
function get_string($string, $start, $end)
{
    $pos = 0;
    $pos = strpos($string, $start, $pos);
    if ($pos === false)
    { // Zero is not exactly equal to false...
        return $found;
    }
    $pos += strlen($start);
    $len = strpos($string, $end, $pos)-$pos;
    $found = substr($string, $pos, $len);
    return $found;
}

$str='<table class="views-table cols-4"><thead><tr><th class="views-field views-field-line-item-title">
        Title          </th>
<th class="views-field views-field-commerce-unit-price">
        Unit price          </th>
<th class="views-field views-field-quantity">
        Quantity          </th>
<th class="views-field views-field-commerce-total views-align-right">
        Total          </th>
</tr></thead><tbody><tr class="odd views-row-first views-row-last"><td class="views-field views-field-line-item-title">
         "Sweet Heart" Package    (SPA02)          </td>
<td class="views-field views-field-commerce-unit-price">
        135.00  CAD          </td>
<td class="views-field views-field-quantity">
        1          </td>
<td class="views-field views-field-commerce-total views-align-right">
 135.00  CAD          </td>
</tr></tbody></table>';

 $str=strtr($str,array("<"=>"&lt;","&"=>"&amp;"));
 echo get_string($str,'class="odd views-row-first views-row-last">&lt;td    class="views-field views-field-line-item-title">','&lt;/td>');
?>

1 个答案:

答案 0 :(得分:1)

使用此功能提取字符串。

function get_string($string, $start, $end)
{
    $pos = 0;
    $pos = strpos($string, $start, $pos);
    if ($pos === false)
    { // Zero is not exactly equal to false...
        return $found;
    }
    $pos += strlen($start);
    $len = strpos($string, $end, $pos)-$pos;
    $found = substr($string, $pos, $len);
    return $found;
}

$str='<table class="views-table cols-4"><thead><tr><th class="views-field views-field-line-item-title">
        Title          </th>
<th class="views-field views-field-commerce-unit-price">
        Unit price          </th>
<th class="views-field views-field-quantity">
        Quantity          </th>
<th class="views-field views-field-commerce-total views-align-right">
        Total          </th>
</tr></thead><tbody><tr class="odd views-row-first views-row-last"><td class="views-field views-field-line-item-title">
         "Sweet Heart" Package    (SPA02)          </td>
<td class="views-field views-field-commerce-unit-price">
        135.00  CAD          </td>
<td class="views-field views-field-quantity">
        1          </td>
<td class="views-field views-field-commerce-total views-align-right">
 135.00  CAD          </td>
</tr></tbody></table>';

 $str=strtr($str,array("<"=>"&lt;","&"=>"&amp;"));//echo $str;
 get_string($str,'class="odd views-row-first views-row-last">&lt;td    class="views-field views-field-line-item-title">','&lt;/td>');