doumi1884 2015-04-08 04:59
浏览 89
已采纳

获取两个字符串PHP之间的子串 - 阅读HTML

I am having a ton of trouble running through finding a string between two strings.

This is the code i currently have

<?

$html = file_get_contents('mywebsite');

$tags = explode('<',$html);

foreach ($tags as $tag)
{
  // skip scripts
  if (strpos($tag,'script') !== FALSE) continue;
  // get text
  $text = strip_tags('<'.$tag);
  // only if text present remember
  if (trim($text) != '') $texts[] = $text;
  
    //print_r($text);
    echo($text);
    
    
}


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

$fullstring = $text;
$parsed = get_string_between($fullstring, "tag1", "tag2");

print_r($parsed);

echo ($parsed);

?>

I think the problem happens on this line:

$fullstring = $text;

I am not entirely sure if $text has the stripped down HTML from the above function. When i run this code i get the stripped out webpage like i expect but i got nothing between the tags i am setting.

Does anyone know why this might be happening or what i am missing?

</div>
  • 写回答

2条回答 默认 最新

  • dqssst0144 2015-04-08 05:31
    关注

    I think its because you are declaring text as a local variable inside for loop. so , after when you are assigning $text to fullstring It's actually null. I don't understand what you are trying to do , but do this and see if it works

    $fullstring = ""
    foreach ($tags as $tag){
        #your code as usual
        echo($text);
        $fullstring = $fullstring.$text;
    }
    

    and delete the $fullstring = $text line.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序