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!

6
  • 1
    There are many tools that helping to build right expression. Type in google "regex online'. Commented Dec 2, 2016 at 23:19
  • 2
    do not use regex to parse html Commented Dec 2, 2016 at 23:26
  • 1
    This is kind of unclear seriously. Bigger text doesn't make it more evident. Commented Dec 2, 2016 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. Commented Dec 3, 2016 at 0: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. Commented Dec 3, 2016 at 0:36

1 Answer 1

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]"
Sign up to request clarification or add additional context in comments.

1 Comment

Nicolas, thanks for your answer! Changed my description of issue after I saw it, pls re read it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.