Possible Duplicate:
PHP, preg_replace, replace tag with tag attr
I read many references but this is very hard to understand.
Please help to improve my code.
I want to replace
<script src="core.js"></script> to <temp src="core.js"></temp>
While I am testing it, below will actual output what I want. However, on $replace, I only put <temp, how come it could replace the end tag???
$string = '<script src="core.js"></script>';
$pattern = '/^<script|<\/script>$/i';
// ^(start from script) | (or) $(end with script)
$replace = '<temp';
echo preg_replace($pattern, $replace, $string);
Out Put
<temp src="core.js"></temp>
Thank you very much for your help and advice.
Sorry if this doesn’t answer your question because you asked about regex, but if you can use str_replace, its generally better to do so, and much quicker.
The problem with this though is, it wont match strings like:
The reason i didn’t just do:
is because you could have other mensions of the word “script” in your variable, for example if a js file was called example.script.js