rapidRoll
rapidRoll

Reputation: 298

Regex to replace tags content

I have a problem with this code:

$message='<ha>hello</ha>';
$message = str_replace('<ha>(.*?)</ha>', '<ha>bye</ha>', $message);
echo $message;

The output is still hello although i want it to be bye..It might be simple but I am using regex for first time. Thanks in advance

Upvotes: 1

Views: 29

Answers (1)

n-dru
n-dru

Reputation: 9430

Use preg_replace function:

$message='<ha>hello</ha>';
$message = preg_replace('/<ha>(.*?)<\/ha>/', '<ha>bye</ha>', $message);
echo $message; // gives: <ha>bye</ha>

Demo:

http://sandbox.onlinephpfunctions.com/code/9b0a2239a891223472e93f8a362e5946e5719df0

Upvotes: 1

Related Questions