2

I need to find out the width & height from the below string

$embed_code = '<iframe id="streamlike_player" name="streamlike_player" marginwidth="0" marginheight="0" src="http://cdn.streamlike.com/hosting/orange-business/embedPlayer.php?med_id=5bad83b03860eab0&width=600&height=391.235955056&lng=fr" frameborder="0" width="600" scrolling="no" height="391"></iframe>';

I am using below to find out width & height but its not giving me exact result I want

preg_match("/width=\"(.*?)\"/", $embed_code, $w_matches);
preg_match("/height=\"(.*?)\"/", $embed_code, $h_matches);

The result is

Array
(
    [0] => width="0"
    [1] => 0
)
Array
(
    [0] => height="0"
    [1] => 0
)

which should be

Array
(
    [0] => width="600"
    [1] => 600
)
Array
(
    [0] => height="391"
    [1] => 391
)

Any one have any idea regarding it? Any help will be appreciated.

Thanks in advance.

Umesh Kulkarni

etuardu
  • 5,066
  • 3
  • 46
  • 58
  • See also: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – johnsyweb Aug 02 '11 at 11:05
  • @Johnysweb Your comment is inappropriate *and should be deleted.* First off, that damned joke answer does not help anybody, **PERIOD**. Even more importantly, you need to pay attention to what the user is doing. He has variables with little snippets of markup in them. There is absolutely nothing whatsoever wrong with using pattern matching on such things. – tchrist Sep 07 '11 at 16:08

5 Answers5

3

The problem is that it matches marginwidth / marginheight instead of width / height. It would be a good idea to add a word boundary before the attributes: \b

preg_match("/\bwidth=\"(.*?)\"/", $embed_code, $w_matches);
preg_match("/\bheight=\"(.*?)\"/", $embed_code, $h_matches);
Karolis
  • 9,396
  • 29
  • 38
3

why to use .*, width are always given as digits if you are not defining them in style. also the regex is matching marginwidth and marginheight first.. you have to do something like this.

preg_match("/ width=\"(\d+)\"/", $embed_code, $w_matches);
preg_match("/ height=\"(\d+)\"/", $embed_code, $h_matches);

give space before width and height in regex. or use word boundary tag \b instead of space.

Aamir Rind
  • 38,793
  • 23
  • 126
  • 164
2

Its probably because it matches the marginwidth="0" marginheight="0" first.

use:

preg_match("/ width=\"(.*?)\"/", $embed_code, $w_matches);
preg_match("/ height=\"(.*?)\"/", $embed_code, $h_matches);
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
  • Hi, I think rather to use below preg_match("/\swidth=\"(.*?)\"/", $embed_code, $w_matches); preg_match("/\sheight=\"(.*?)\"/", $embed_code, $h_matches); – Umesh Kulkarni Aug 02 '11 at 10:52
1

Your regex finds marginwidth and marginheight cause you included the quotation marks.

Try:

preg_match("/width=(\d+)/", $embed_code, $w_matches);
preg_match("/height=(\d+)/", $embed_code, $w_matches);

EDIT:

Oha, I missed the explicit width and height attributes at the end of your string (scrolled off). My regexes match these:

ab0&width=600&height=391.23595

Kaken Bok
  • 3,395
  • 1
  • 19
  • 21
0

The dead simplest solution is to include the space in front of the word you want to match (ie: match in ' width' rather than 'width').

The flavor of regular expression you use may also support word boundaries, something like \W which means "match only a non-word character" or b which means "beginning of a word". In this case you want to match "any non-word character followed by 'width", for example \Wwidth=... or \bwidth=...

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • It is an error to call `\b` “beginning of word”. For example, in the pattern `/foo\b/`, that `\b` is definitely not a “beginning of word”. Rather, it’s always a word-boundary transition point. It is precisely equivalent to `(?:(?<=\w)(?!\w)|(?<!\w)(?=\w))`, and for the record `\B` is precisely equivalent to `(?:(?<=\w)(?=\w)|(?<!\w)(?!\w))`. – tchrist Sep 07 '11 at 16:09