Regex & PHP - isolate src attribute from img tag

前端 未结 10 1451
眼角桃花
眼角桃花 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:58

    lets assume i use

    $text ='blabla';
    

    in

    getTextBetween('src="','"',$text);
    

    the codes will return :

    blabla.jpg" alt="blabla" 
    

    which is wrong, we want the codes to return the text between the attribute value quotes i.e attr = "value".

    so

      function getTextBetween($start, $end, $text)
                {
                    // explode the start string
                    $first_strip= end(explode($start,$text,2));
    
                    // explode the end string
                    $final_strip = explode($end,$first_strip)[0];
                    return $final_strip;
                }
    

    does the trick!.

    Try

       getTextBetween('src="','"',$text);
    

    will return:

    blabla.jpg
    

    Thanks all the same , because your solution gave me an insight to the final solution .

提交回复
热议问题