MoFish Posted March 27, 2022 Share Posted March 27, 2022 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" /> Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/ Share on other sites More sharing options...
gw1500se Posted March 27, 2022 Share Posted March 27, 2022 What have your tried? Post the code here (use the code icon(<>) in the top menu and specify PHP. If you don't know where to start read this. Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594628 Share on other sites More sharing options...
MoFish Posted March 28, 2022 Author Share Posted March 28, 2022 $success = preg_match_all("/{% snippet '(.*?)' %}/i", $html, $results); if ($success) { print_r($results); } Not very far; i got as far as finding the lines of code in the html. Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594634 Share on other sites More sharing options...
Barand Posted March 28, 2022 Share Posted March 28, 2022 Try $html2 = str_replace( ['{% snippet ', '%}'], ['<x-snip name=', '/>'], $html); 1 Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594637 Share on other sites More sharing options...
MoFish Posted March 28, 2022 Author Share Posted March 28, 2022 @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); Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594638 Share on other sites More sharing options...
Barand Posted March 28, 2022 Share Posted March 28, 2022 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" /> 1 Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594640 Share on other sites More sharing options...
MoFish Posted March 28, 2022 Author Share Posted March 28, 2022 @BarandThank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594641 Share on other sites More sharing options...
MoFish Posted March 28, 2022 Author Share Posted March 28, 2022 (edited) @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) Edited March 28, 2022 by MoFish Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594646 Share on other sites More sharing options...
Barand Posted March 28, 2022 Share Posted March 28, 2022 As far as I know, self closing tags have been consigned to history and are no longer required. Just use ">" instead of "/>" Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594649 Share on other sites More sharing options...
MoFish Posted March 28, 2022 Author Share Posted March 28, 2022 (edited) @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 March 28, 2022 by MoFish Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594650 Share on other sites More sharing options...
ginerjm Posted March 28, 2022 Share Posted March 28, 2022 (edited) $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 March 28, 2022 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594651 Share on other sites More sharing options...
MoFish Posted March 28, 2022 Author Share Posted March 28, 2022 (edited) @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 March 28, 2022 by MoFish Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594655 Share on other sites More sharing options...
ginerjm Posted March 28, 2022 Share Posted March 28, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314632-string-replacement-help/#findComment-1594656 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.