Jump to content

Recommended Posts

Hi,

I have some html stored in a variable called $html

Inside this $html variable I have the following content {% snippet "footer" %}

I would like to find all instances of this tag in the $html and replace it with <x-snip name="footer" />

Could anyone advise me on how this would be best achieved?

Thank you,

MoFish

--

Ideally this would work for any number of these custom tags e.g.

{% snippet "footer" %} and {% snippet "header" %} would become <x-snip name="footer" /> and <x-snip name="header" />

Link to comment
https://forums.phpfreaks.com/topic/314632-string-replacement-help/
Share on other sites

@BarandThank you very much; that is exactly what I was looking for and works perfectly. Quick question:

Is there a more elegant way to do multiple replacements? The below code does work; but looks a little unwealdy.

$html = str_replace( ['{% snippet ', '%}'], ['<x-snippet name=', '/>'], $page->template->source);
$html = str_replace( ['{% collection ', '%}'], ['<x-collection name=', '/>'], $html);
$html = str_replace( ['{% setting ', '%}'], ['<x-setting name=', '/>'], $html);

 

str_replace takes an array of search string and a corresponding array of replacement strings, so...

$source = '
    {% snippet "header" %}
    yada yada
    {% setting "X" %}
    yada yada
    {% collection "stamps" %}
    blah blah
    {% snippet "footer" %}
    ';

$html = str_replace( [  '{% snippet ', 
                        '{% collection ', 
                        '{% setting ', 
                        '%}'
                     ], 
                     [  '<x-snippet name=',
                        '<x-collection name=',
                        '<x-setting name=',
                        '/>'
                     ], 
                     $source );

echo '<pre>' . htmlentities($html) . '</pre>';

Giving...

    <x-snippet name="header" />
    yada yada
    <x-setting name="X" />
    yada yada
    <x-collection name="stamps" />
    blah blah
    <x-snippet name="footer" />

 

  • Like 1

@Barand Sorry to bother you.

Everything was going great until I came across the last couple tags I needed to replace which were not self closing like the others (example 4 and example 5 below) which caused issues.

Can you think of a way around this problem? Or is there some regex solution which can pick out the first and last parts whilst skipping the word in the quotes? 🤢

{% snippet name="hello1" %}
// OK
<x-snippet name="hello1" />

{% setting name="hello2" %}
// OK
<x-setting name="hello2" />

{% collection name="hello3" %}
// OK
<x-collection name="hello3" />

{% extend "boom" %}
// Issue with />
<x-template name="boom">
  
{% endextend %}
// Issue with />
<x-template>

Code

$source = "{% snippet name='hello1' %} {% setting name='hello2' %} {% collection name='hello3' %} {% extend 'boom' %} {% endextend %}";
$html = str_replace([
    '{% snippet ', 
    '{% collection ', 
    '{% setting ', 
    '{% extend ', 
    '{% endextend ', 
    '%}'
], 
[ 
    '<x-snippet name=',
    '<x-collection name=',
    '<x-setting name=',
    '<x-template name=',
    '</x-template ',
    '/>'
], 
$source);
dump($html);

Result (see areas in red which are incorrect tags)

image.png.b0861fbaf4c3e723532b6cfa582438a4.png

Edited by MoFish

@BarandI tried that, It look's like laravel components which im using to render them out; need the trailing /> in order to work correctly 😠

Can some regex magic be done in this scenario or is str_replace the way forward?

Thanks so much for your help.

Edited by MoFish
$result = str_replace(['abc','def','xyz'], ['ABC', 'DEF', 'XYZ'], $string);

The above will change "abc, def, xyz" to:   "ABC, DEF, XYZ";

My $.02.   Str_replace will do an exact replacement of literal strings of characters.  This is regardless of whether they are 'tags' or not.

Edited by ginerjm

@ginerjm I think that's the problem; I'm trying to treat them all the same when they probably should be a little more unique. They slightly vary which is throwing me.

{% snippet 'hello' %} to be <x-snippet name="hello" /> with a self closing tag

{% extend 'boom' %} to be <x-template name="boom"> without a self closing tag

{% endextend %} with the self closing tag </x-template>

Edited by MoFish

Since one fix has nothing to do with another one I"m not sure what your problem is.  You are simply trying to change string x to string y.  Where's the confusion?  And as pointed out already if these are all HTML tags then you don't need the /> close nor the </ you are using as an opening tag perhaps.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.