MarkP
MarkP

Reputation: 2566

PHP: String replace with Regex

Ive tried to get the Regex to work for this script.

What I need it to do is take the width and height from the youtube code that is inserted and replace it with a specified height and width.

$youtube = str_replace('width="' * ' "','width="500"' $_POST['youtube']);

Can anyone help with this?

Upvotes: 0

Views: 133

Answers (1)

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

Assuming your code is:

$code = 'width="100" and height="200"';

And then perform the below regex replace using preg_replace()

$code = preg_replace('/width="\d+"/', 'width="XXX"', $code);
$code = preg_replace('/height="\d+"/', 'height="XXX"', $code);

\d+ means any number of digit starting from length one.

Upvotes: 3

Related Questions