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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化