this is my string
$var = 'foo<ul class="foo"> test </ul>foo<ul class="bar">foo</ul><ul></ul>foo';
this is my regex
/<ul[^>]*>.*?<\/ul>/g
this is my code
$var = preg_replace('/<ul[^>]*>.*?<\/ul>/', 'bar', $var);
i want to replace all ul elements to bar so after this code my string should look like this
foo barfoo bar barfoo
It works in regex101.com perfectly, but doesn't replace anyting in php Any ideas what's wrong with my code or regex?
//edit
After answers, I tried this and worked then I realized my original string had newlines in it. So I changed my regex to this and it works now
/<ul[^>]*>(.||
)*?<\/ul>/g
Thanks for your answers :)
Well, I had those code tested and it turned out OK:
$var = 'foo<ul class="foo"> test </ul>foo<ul class="bar">foo</ul><ul></ul>foo';
$var = preg_replace('/<ul[^>]*>.*?<\/ul>/', 'bar', $var);
echo $var;
please try it.
To get the result as you mentioned in your question foo barfoo bar barfoo
try following
<?php
$var = 'foo<ul class="foo"> test </ul>foo<ul class="bar">foo</ul><ul></ul>foo';
$var = preg_replace('/<ul[^>]*>.*?<\/ul>/', ' bar', $var);
echo $var;
?>
I added a space in replace string ' bar'
Hope this helps.