{"id":122,"date":"2009-07-07T11:10:49","date_gmt":"2009-07-07T05:40:49","guid":{"rendered":"http:\/\/vikku.info\/codesnippets\/?p=122"},"modified":"2009-07-08T02:32:34","modified_gmt":"2009-07-07T21:02:34","slug":"replace-width-and-height-of-html-element-using-regular-expression","status":"publish","type":"post","link":"http:\/\/vikku.info\/codesnippets\/php\/replace-width-and-height-of-html-element-using-regular-expression\/","title":{"rendered":"replace width and height of html element using regular expression"},"content":{"rendered":"<p>php code to replace the width and height of any html tag using php and regular expression.<\/p>\n<p>i had a youtube listing for the admin where i display 10 per page but usually the object tag width and height will be around 400&#215;350 approximately.<br \/>\nbut i want to show the youtube video small in the admin section so that it will match the size of each row which will have edit, delete buttons&#8230;<br \/>\nhere i wished the width and height to be around 100&#215;80. <\/p>\n<p>so when i display the youtube object tag code from the data base i used the following regular expression to replace the original width and height with my preferred value.<\/p>\n<p>and here is the code to do the width and height replacement. it works for me and hope for you too&#8230;<\/p>\n<pre lang=\"php\">\r\n$pattern = \"\/height=\\\"[0-9]*\\\"\/\";\r\n$string = preg_replace($pattern, \"height='120'\", $rs['url']);\r\n$pattern = \"\/width=\\\"[0-9]*\\\"\/\";\r\n$string = preg_replace($pattern, \"width='200'\", $string);\r\necho $string;\r\n<\/pre>\n<p>we have another alternate for the above &#8230;<\/p>\n<p> you can write the above code in one line by using alternation<\/p>\n<pre lang=\"php\">\r\n\r\n$pattern = '\/(width|height)=\"[0-9]*\"\/i';\r\n\/\/\r\n<\/pre>\n<p>you can use single-quotes instead of double-quotes to reduce the need to escape characters like double-quotes and backslashes.<\/p>\n<p>the input was like this&#8230;<\/p>\n<pre lang=\"text\">\r\n<object width=\"425\" height=\"344\">\r\n<param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/GwQMnpUsj8I&#038;hl=en&#038;fs=1\">\r\n<\/param><param name=\"allowFullScreen\" value=\"true\">\r\n<\/param><param name=\"allowscriptaccess\" value=\"always\">\r\n<\/param><embed src=http:\/\/www.youtube.com\/v\/GwQMnpUsj8I&#038;hl=en&#038;fs=1 \r\ntype=\"application\/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\">\r\n<\/embed><\/object> \r\n<\/pre>\n<p>and the output will be like this&#8230;<\/p>\n<pre lang=\"text\">\r\n<object width=\"200\" height=\"120\">\r\n<param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/GwQMnpUsj8I&#038;hl=en&#038;fs=1\">\r\n<\/param><param name=\"allowFullScreen\" value=\"true\">\r\n<\/param><param name=\"allowscriptaccess\" value=\"always\">\r\n<\/param><embed src=http:\/\/www.youtube.com\/v\/GwQMnpUsj8I&#038;hl=en&#038;fs=1 \r\ntype=\"application\/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"200\" height=\"120\">\r\n<\/embed><\/object> \r\n<\/pre>\n<p>there are two width and height attributes which got replaced&#8230; one is in the first line and next is for the embed tab inside.<\/p>\n<p>&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php code to replace the width and height of any html tag using php and regular expression. i had a youtube listing for the admin where i display 10 per page but usually the object tag width and height will be around 400&#215;350 approximately. but i want to show the youtube video small in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/122"}],"collection":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":11,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":133,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/122\/revisions\/133"}],"wp:attachment":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}