2015-01-17 2 views
0

У меня есть строка 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), как мне это сделать наиболее эффективно?

Благодаря

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

Как вы его разобрали? Это форма? –

+0

@cerr вы хотите удалить его или что? – acontell

+0

@acontell Я хотел бы извлечь его, удалить все остальное .... – cerr

ответ

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

Спасибо, но это почему-то не работает корректно. См. ** РЕДАКТИРОВАТЬ 1 ** выше – cerr

Смежные вопросы