Regex & PHP - isolate src attribute from img tag

前端 未结 10 1439
眼角桃花
眼角桃花 2020-11-28 08:55

With PHP, how can I isolate the contents of the src attribute from $foo? The end result I\'m looking for would give me just \"http://example.com/img/image.jpg\"



        
10条回答
  •  爱一瞬间的悲伤
    2020-11-28 09:48

    You can go around this problem using this function:

    
    function getTextBetween($start, $end, $text)
    {
     $start_from = strpos($text, $start);
     $start_pos = $start_from + strlen($start);
     $end_pos = strpos($text, $end, $start_pos + 1);
     $subtext = substr($text, $start_pos, $end_pos);
     return $subtext;
    }
    $foo = 'test image';
    $img_src = getTextBetween('src="', '"', $foo);

提交回复
热议问题