Good Max
Good Max

Reputation: 11

PHP regular expression [ replace ]

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!

Upvotes: 1

Views: 86

Answers (1)

Nicolas
Nicolas

Reputation: 7081

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]"

Upvotes: 1

Related Questions