Search code examples
phpjavascripthtmlshortcode

replace text inside some text with other


I created some shortcodes for some images but when page loads I want to replace it with a img html line.

Is there any way to do it?

Or maybe get all the text in that textarea and replace the [something]asd[/something] with the code I want to show in the end.

All im doing is using a library that when we click and image it puts the next text on the textarea.

[item=myitem]

Then I want to replace this with:

<img class="itemtooltip" alt="" src="myitem.png" width="64" height="64" data-tip="myitem" />

the replacement process is on another page. I have all the text area on a string so I want to search in that text and replace


Solution

  • $var = '[item=myitem]';
    $new = '<img class="itemtooltip" alt="" src="';
    $new2 = '.png" width="64" height="64" data-tip="myitem" />';
    
    $var = str_replace('[item=',"", $var);
    $var = rtrim(trim($var), ']');
    echo $new.$var.$new2;