You tube height and width changing

preg_replace is what you’re after.

For example:


$youTube = preg_replace('/(width|height)(="\\d+")/', '$1="999"', $youTube);

That hasn’t been tested with live code, but it works on the Regular Expression Tester so it should be pretty good.

The next question is, are there regex patterns that are “better” than the above? I’m thinking that non-captured groups would do a better job.

you are changing both width and height with same value.But it is always different

They always start with different values. That is the power of a regular expression.

“\d+” will match 1 or more digits inside the double quotes. Those digits will be replaced with 999, or whatever number you decide to use.

For example:

before: width=“345” height=“274”
after: width=“999” height=“999”

before: width=“193” height=“800”
after: width=“999” height=“999”

before: width=“987” height=“1”
after: width=“999” height=“999”

So use two rules. If you want someone to code it for you, head to the market place. If you have a problem with some regex you have written feel free to post it here.

I think he’s worried that they start out as being different values, and is having a difficult time realising that regular expressions solve that problem.

I have come up with a solution like this but it fails in one particular video

$u = $paths_youtube;
$m=preg_split(‘/width=/’,$u);
echo $org_w = substr($m[1],0,5);
echo “<br/>”;
echo $org_h = substr($m[1],13,5);

$u = str_replace(‘width=’.$org_w, ‘width=“260”’, $u);
$u = str_replace(‘height=’.$org_h, ‘height=“207”’, $u);

echo $u;

Here’s another dosing of regular expressions.

$youTube = preg_replace(‘/width=“(\d+)”/’, ‘260’, $youTube);
$youTube = preg_replace(‘/height=“(\d+)”/’, ‘207’, $youTube);

Thanks to all