-1

My string contains

    [[image.jpg(alt)]]

And I need replacement

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

My working regex

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

This Regex is working but i want a better Regex with additional values(example:width)

I need this string

    [[image.jpg|alt|100]]

Converted to this

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

With Regex

    I seek help for this 
    from you guys
Jitu
  • 57
  • 7

1 Answers1

0

Mabe you should simply use explode ?

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

Edit: Quick example. Assuming your tags are valid. Use Regex to extract tags, then juste explode it to have values.

$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);
}
Will
  • 899
  • 8
  • 15
  • I like regex ..And is it posible to use explode if the string is like this? "normal paragraph [[image.png|alt|100]] anothe normal paragraph ........... – Jitu Apr 07 '20 at 15:23
  • Regex are often an overkill solution, use it only if you don't have an other option. Answer updated with full example for extracting tags and then your data; – Will Apr 07 '20 at 15:35