-3

I would like to change in all images the value of data-src to src with regular expressions and considering that it may be before or after the src, That is, you never know your place.

Example:

<img width="50" src="old.jpg" height="50" data-src="new.jpg">

to

<img width="50" src="new.jpg" height="50" data-src="new.jpg">

Please PHP and regular expressions!!

Thanks very much

halfer
  • 19,824
  • 17
  • 99
  • 186
jcarlosweb
  • 846
  • 1
  • 14
  • 30
  • Are these image tags the full string, or are they substrings contained in a larger string? – mickmackusa Jun 02 '17 at 08:18
  • @quentin Incredible, duplicate? So bad I express myself? I'm asking how to do with regular expressions, I'm not asking which is the best way to do it and neither the different ways of doing this, I am asking a special case, and I assure you that many people will ask. – jcarlosweb Jun 02 '17 at 08:32
  • Several of the answers on the duplicate show how to use regular expressions on HTML. – Quentin Jun 02 '17 at 08:41
  • I am not asked how to use it, then using your method, all the specific questions would be duplicated, since all the questions can be linked to how it is used, example: question about jquery, you send it to the jquey page because it explains how it's used!!! So why does stackoverflow exist? – jcarlosweb Jun 02 '17 at 08:49
  • Advice for posting: show what effort you have made in your question, demonstrate that you know Stack Overflow is not a free coding platform, and keep voting commentary to the comments rather than editing it into posts. – halfer Jun 02 '17 at 14:42
  • Quentin I'm going to unsubscribe, and I want to delete all my questions. You can continue to add negatives and put the duplicates you want, this has seemed a joke of bad taste on your part. – jcarlosweb Jun 02 '17 at 16:39
  • @jcarlosweb: you can delete your account if you wish, though that will leave the questions in place. My recommendation however is to take a break for a day or two, don't worry about getting some downvotes, and resolve to ask a better question next time. Stack Overflow does take a bit of getting used to, since tech forums have historically allowed pretty much anything to be on topic. We are focussed on building a collection of questions that will be useful for a wide audience here, and not everything will make the cut. That's OK. – halfer Jun 02 '17 at 20:14
  • (If you wish to reply to a specific person, then use their handle please, e.g. `@halfer`, `@Quentin`, etc). – halfer Jun 02 '17 at 20:14

1 Answers1

1

Since you want a php regex solution...

My pattern will match, regardless of attribute position, the data-src value and use it as the replacement value for both src and data-src. It will unfortunately omit other attributes. This may or may not be an issue for your project. If it is, please update your question with a better representing sample input.

/<img[^>]+data-src=("[^"]+")[^>]*>/

Pattern Demo

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Thank you @mickmackusa , we are very near, and exactly, we do not know the place of the data-src, Could be achieved by maintaining the attributes? – jcarlosweb Jun 02 '17 at 08:13