如何使用regex-php制作html图像标记



我的字符串包含

[[image.jpg(alt)]]

我需要更换

<img src="image.jpg" 
alt="alt"/>

我的工作正则表达式

/[[(.*?).{1} 
(jpg|png|jpeg|gif) 
((.*?))]]/s

这个Regex正在工作,但我想要一个更好的Regex与附加值(例如:宽度(

我需要这个字符串

[[image.jpg|alt|100]]

转换为此

<img src="image.jpg' 
style="width:100px" 
alt="alt"/>

使用Regex

I seek help for this 
from you guys

是否应该简单地使用爆炸?

explode('|', 'image.jpg|alt|100');

编辑:快速示例。假设您的标签有效。使用Regex提取标记,然后将其分解为具有值。

$text = 'qesrtdhyfugihseqs [[image.jpg|alt|100]] zqrestduhzeqmo qzeomphuzer qzeirgh [[image2.jpg|alt2|200]]';
preg_match_all('/[[(.*)]]/U', $text, $matches);
foreach ($matches[1] as $image) {
$imageAttributes = explode('|', $image);
var_dump($imageAttributes);
}

最新更新