Regular expression to replace an with respective

后端 未结 3 1768
一个人的身影
一个人的身影 2021-01-17 01:50

I\'m looking for a PHP preg_replace() solution find links to images and replace them with respective image tags.

Find:



        
3条回答
  •  悲&欢浪女
    2021-01-17 02:10

    Congratulations, you are the one millionth customer to ask Stack Overflow how to parse HTML with regex!

    [X][HT]ML is not a regular language and cannot reliably be parsed with regex. Use an HTML parser. PHP itself gives you DOMDocument, or you may prefer simplehtmldom.

    Incidentally, you cannot tell what type a file is by looking at its URL. There is no reason a JPEG has to have ‘.jpeg’ as its extension — and indeed, no guarantee that a file with ‘.jpeg’ extension will actually be JPEG. The only way to be certain is to fetch the resource (eg. using a HEAD request) and look at the Content-Type header.

提交回复
热议问题