-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
3
  • Why not use the same mechanism? stackoverflow.com/questions/61071810/… Commented Apr 7, 2020 at 15:10
  • May be it will create conflicts with eachother using the same mechanism? Commented Apr 7, 2020 at 15:18
  • How do you know the second value is alt and the third one is style for width? What are the specs? Commented Apr 8, 2020 at 8:00

1 Answer 1

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

2 Comments

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 ...........
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;

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.