So I have a working preg_match pattern to get the value of an HTML attribute, and let me do this Need to talk with Preg_replace - do not find the value and return it, but replace it with a new one.
But if I just use the preg_match pattern ... I'm stuck.
Here is the code:
$ string = '& lt; Object class = "clayed: D27CDB6E-AE6D-11cf-96B8-444553540000" width = "437" height = "288" id = "Viddler" & gt; & Lt; Param name = "movie" value = "http://www.viddler.com/player/1234567890/" /> & Lt; Param name = "allowScriptAccess" value = "always" /> & Lt; Param name = "allowFullScreen" value = "true" /> & Lt; Param name = "flashvars" value = "counterfeit = 1" /> & Lt; Embed src = "http://www.viddler.com/player/1234567890/" width = "437" height = "288" type = "app / x-shock-v-flash" allowScriptAccess = "always" allow FullScreen = "True" FlashWare = "Fake = 1" name = "Widler" & gt; & Lt; / Embed & gt; & Lt; / Object & gt; '; Preg_match ("| <| ^ [^ & gt;] * width = \" ([^ & gt;] +) \ "[^ & gt;] * & gt; | UI", $ string, $ result) ; Echo 'width:' $ Result [1]; This works and will return 437 . If I use the same pattern in a preg_replace () function, then this whole & lt; Object & gt; will erase the tag I think 437 needs to replace with something else, say 200. And when we are on it, then I need to change the value of height too. In the end, it should work for any embed code or iframe, so it should be used only for width = " or Height = " should be shown for ... ... just like the preg_match example
Edit: below Tim's solution I found Tim's pattern with a little revision: $ interm = preg_replace ('| (<; [^ & gt; ;] * Width =) "[^ & gt;] +" ([^ & gt;] * & gt;) | Ui ',' \ 1 "200" \ 2 ' , $ String); $ [Result = preg_replace ('| (|' [*] * height =) "[^ & gt;] +" ([^ & gt;] * & gt;) | UI ',' 1 \ "300 \ \ 2 ', $ Interm); Without this change, the first function takes the place of the width, but in the tag every feature is taken out after that.
Thanks for Tim for the solution!
You about You have to capture the transfer string: Since you want to change the height and width with different values They are, therefore, you need two regdes, have been implemented sequentially: $ interm = 'Preg_replace' ('/ (& lt; * [^ & gt;] * Width =) "[^ & gt;] +" ([^ & gt;] * & gt;) / ',' \ 1 "200" \ 2 ', $ subject); $ Result = preg_replace (' / (& Lt; * [^ & gt;] * height =) "[^ & gt;] +" ([^ & gt;] * & gt;) / ',' \ 1 "300" \ 2 ', $ interim) ;
Comments
Post a Comment