Home >Backend Development >PHP Tutorial >How to use regular php to copy src and put it into another attribute of this tag
has characters
xxxx<img src=“a.jpg”>xxxx<img src=“b.jpg”>.......
If there are many words like this add img
tag
How to use regular expressions to change all img
tags into
xxxx<img src=“a.jpg” data-url="a.jpg">xxxx<img src=“b.jpg” data-url="b.jpg">.......
has one more data-url
, and its value is equal to src
has characters
xxxx<img src=“a.jpg”>xxxx<img src=“b.jpg”>.......
If there are many words like this add img
tag
How to use regular expressions to change all img
tags into
xxxx<img src=“a.jpg” data-url="a.jpg">xxxx<img src=“b.jpg” data-url="b.jpg">.......
has one more data-url
, and its value is equal to src
If it is the same attribute value, then can you just get the src attribute directly when getting it? I don’t understand what your needs are
But if you must use PHP regular implementation, you can also use the preg_replace_callback method to replace it. Use regular rules to match src="a.jpg" and replace it with src="a.jpg" data-url=" a.jpg" will do
Can I use jq? The following is the jq method
<code><img src="1242x225.jpg" /> <script type="text/javascript"> $("img").each(function(i){ var img_url = $('img')[i].src; var img_name = img_url.substring(img_url.lastIndexOf("/")+1,img_url.length); $(this).attr("data-url",img_name); }); </script> 以下是浏览器打印效果 </code>