简体   繁体   English

特定单词的正则表达式

[英]Regular Expression for specific word

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 Umesh Kulkarni

The problem is that it matches marginwidth / marginheight instead of width / height . 问题是它匹配marginwidth / marginheight而不是width / height It would be a good idea to add a word boundary before the attributes: \\b 在属性之前添加单词边界是个好主意: \\b

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

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. 正则表达式首先匹配marginwidth和marginheight ..你必须做这样的事情。

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. 或使用单词边界标记\\ b而不是空格。

Its probably because it matches the marginwidth="0" marginheight="0" first. 这可能是因为它首先匹配marginwidth =“0”marginheight =“0”。

use: 采用:

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

Your regex finds marginwidth and marginheight cause you included the quotation marks. 你的正则表达式找到marginwidth和marginheight,因为你包括了引号。

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 ab0&width = 600 &height = 391 .23595

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'). 最简单的解决方案是在要匹配的单词前面加上空格(即:匹配'width'而不是'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". 您使用的正则表达式的味道也可能支持单词边界,类似\\W表示“仅匹配非单词字符”或b表示“单词的开头”。 In this case you want to match "any non-word character followed by 'width", for example \\Wwidth=... or \\bwidth=... 在这种情况下,您希望匹配“任何非单词字符后跟'宽度”,例如\\Wwidth=...\\bwidth=...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM