Regular expression to find “src” attribute of HTML “img” element in PHP

前端 未结 4 2009
借酒劲吻你
借酒劲吻你 2020-12-10 20:04

I have a string, inside of that I have an image:

\"

<

相关标签:
4条回答
  • 2020-12-10 20:22

    Thank every one for helping me out.

    I found my solution by using:

    pattern = "/src=([^\\\"]+)/"
    
    0 讨论(0)
  • 2020-12-10 20:32

    Here is an easy way to match <img /> tag src attribute and or it content in html/PHP with regular expression.

    Sample:

    <img class="img img-responsive" title="publisher.PNG" src="media/projectx/agent/author/post/2/image/publisher.PNG" alt="" width="80%" />
    

    To match just src attribute content use

    preg_match("%(?<=src=\")([^\"])+(png|jpg|gif)%i",$input,$result)
    

    $result[0] will output media/projectx/agent/author/post/2/image/publisher.PNG

    To match `src' attribute and it content use

    preg_match("%src=\"([^\"])+(png|jpg|gif)\"%i",$input,$result)
    

    $result[0] will output src="media/projectx/agent/author/post/2/image/publisher.PNG"

    0 讨论(0)
  • 2020-12-10 20:36

    The parts that reads ([^\s]+) means select anything that isn't a space.

    Maybe try something like:

    /src="([^"]+)"/
    

    Which is select anything that isn't a double quote.

    0 讨论(0)
  • 2020-12-10 20:38

    I'd catch everything inside the quotes:

    preg_match_all('/src="([^"]+)"/', $questArr_str, $images);
    
    0 讨论(0)
提交回复
热议问题