aniltc:
hi
you have mentioned like this
$u = str_replace(‘width=“425”’, ‘width=“999”’, $u);
$u = str_replace(‘height=“344”’, ‘height=“999”’, $u);
width=425;but in my case it is not fixed any width can come form you tube.
that is the real issue
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.
aniltc
February 11, 2010, 11:26am
22
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”
hash
February 11, 2010, 11:32am
24
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.
aniltc
February 11, 2010, 12:03pm
26
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);