1

Ask to help with two cases with regular expressions in html code edition [ parsing ]

Now I use parser with the next logic (example below with YouTube)

select the unique fragment (ED96RtfF22E) of source html code from YouTube and put it into internal tag [YouTube] on my website

SOURCE :

<iframe width="560" height="315" src="https://www.youtube.com/embed/ED96RtfF22E" frameborder="0 "allowfullscreen> </iframe> 

USE :

replace|0|#<iframe[^>]*youtube\.com\/embed\/([^"]*)"[^>]*>[^<]*<\/iframe>#|[YouTube]$1[/YouTube]|1|

based on this logic need to solve issue below:

Case #1

replacement the part of html code with images

SOURCE: have incorrect tale after .jpg (?12345)

<img alt="" src="xxx.com/ooo.jpg?12345"> 

NEED: "src" tag without this tale like this <img alt="" src="xxx.com/ooo.jpg">

replace|0|...|...|1|

parts marked here as "..." is a places for php regular expressions for changes. I would be grateful for your help!

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
Good Max
  • 11
  • 3
  • 1
    There are many tools that helping to build right expression. Type in google "regex online'. – Tomasz Winter Dec 02 '16 at 23:19
  • 2
    [do not use regex to parse html](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – JazZ Dec 02 '16 at 23:26
  • 1
    This is kind of unclear seriously. Bigger text doesn't make it more evident. – Nicolas Dec 02 '16 at 23:59
  • Why use regex for these very specific functions? The first one could simply be gotten using `explode()` and `array_reverse()` and the second one is a simple `explode()` - granted you would need to get the `src`-element, and for that there are several methods, but regex is not something I would use for such simple tasks. – junkfoodjunkie Dec 03 '16 at 00:11
  • There's no need to bold and header-ify large parts of your question. It's not going to make us more likely to answer your question. – ceejayoz Dec 03 '16 at 00:36
  • No problem) Just want to make text more convenient for viewing – Good Max Dec 03 '16 at 00:40

1 Answers1

1

For first case: <img.*src=\"[^?]*).*(\">) and replace with \1\2.


For second case: <iframe.*src=\".*embed\/(.*?)\".*?<\/iframe> and replace with [YT]\1[/YT].

If you don't want to replace then use:

preg_match('/<iframe.*src=\".*embed\/(.*?)\".*?<\/iframe>/', $input, $match);
$output = "[YT]" + $match[1] + "[/YT]"
Nicolas
  • 6,611
  • 3
  • 29
  • 73