I have some HTML
, I want to replace this with some other html. The username is a variable. How can I do this this? I know I'll use the preg_replace function and know how to add the new html but need to detect the string first and get the username as a variable. $str = preg_replace('#[Quote User=\".*?>(.*?)\"]#i', 'Something new......', $str); (Added code block mark-up ~ MOD)
, I want to replace this with some other html.
The username is a variable. How can I do this this? I know I'll use the preg_replace function and know how to add the new html but need to detect the string first and get the username as a variable.
$str = preg_replace('#[Quote User=\".*?>(.*?)\"]#i', 'Something new......', $str);
(Added code block mark-up ~ MOD)
nzkiwi80 detect the string first and get the username as a variable.
Pretty much answered your own question there. preg_match would be needed to first get the username into a variable before it's removed.
(We of course may never know what the original HTML looked like.)
You, ah, wanna try that again? This forum uses `​``tripled backticks​``` to mark up code.
Looks like you need to escape the square brackets with back-slashes, since they have special meanings in regex expressions.
Just a thought, and it might not apply here, but in general, parsing HTML is much simpler with the PHP DOMDocument class than with regular expressions.