提取定义位置的子字符串


extract substring at defined position

>我有一个 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() 无法在$string中找到$start,这很奇怪,因为我将它们都打印到屏幕上,我可以看到,内容肯定在字符串中!

<?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>');
?>

使用此函数提取字符串。

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>');