Regular expression for extracting the value of the SRC attribute of an IMG element

Source: Internet
Author: User
This article introduces you to the link address of the SRC attribute value if you get the picture from IMG.

The string to match:


Regular Expressions:

[^ '" "]*) [\ '"]) | (? <src>[^\s]*))

Regular match output results:

Ximage/ad1.gifimage/ad2.gif

PHP regular extract or the SRC attribute value of the IMG element:

<?php/*php Extract any attribute in the IMG tag */$str = ' <center>php extract or change any of the attributes in the IMG tag of the image </center> '; 1, take the entire picture code Preg_match ('/<\s*img\s+[^>]*?src\s*=\s* (\ ' |\ ') (. *?) \\1[^>]*?\/?\s*>/i ', $str, $match); echo $match [0]; 2, take width preg_match ('//i ', $str, $match); echo $match [1]; 3, take height Preg_match ('//i ', $str, $match); echo $match [1]; 4, take src preg_match ('//i ', $str, $match); echo $match [1]; /*php Replace any attribute *///1 in the image img tag, replace src= "/uploads/images/20100516000.jpg" with src= "/uploads/uc/images/20100516000.jpg") Print Preg_replace ('/()/I ', "\${1}uc/images/\${3}", $STR); echo "

JS Regular expression Extract image address

Regular Expressions <script language= "JavaScript" >
var a= ' <p></P>"
var b=//gi var s=a.match (b) for (Var i= 0;i<s.length;i++) {alert (s[i]); alert (regexp.$1)} </script>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.