mbk Posted November 19, 2009 Share Posted November 19, 2009 Hi, I have the following html code and need to extract only the piece after the rvo_model - so in this case MFC5860CNU1 <script type="text/javascript"> rvo_retailer_ref='msc'; rvo_manufacturer='Brother'; rvo_model='MFC5860CNU1'; rvo_format='image_horizontal310x70'; </script> I am sure I will need to use Regex but as I am only just learning about egex this is too complicated for me at present. Edit: I forgot to add that there are other scripts within the rest of the html, so when I tried to capture text just between the script tags, I got a whole load of results. This is however the only place that has the rvo_model info. Can someone else help please? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/ Share on other sites More sharing options...
rajivgonsalves Posted November 19, 2009 Share Posted November 19, 2009 something like this should do <?php $str = <<< EOF <script type="text/javascript"> rvo_retailer_ref='msc'; rvo_manufacturer='Brother'; rvo_model='MFC5860CNU1'; rvo_format='image_horizontal310x70'; </script> EOF; preg_match_all("#rvo_model='(.*?)';#", $str, $matches); print_r($matches[1]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960894 Share on other sites More sharing options...
mbk Posted November 19, 2009 Author Share Posted November 19, 2009 Rajiv, Thanks for the quick response. The definitions of rvo_manufactuer and rvo_model change all the time, so I need to find a way of being able to recognise this has changed, but you have given me a starting place Thanks very much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960900 Share on other sites More sharing options...
rajivgonsalves Posted November 19, 2009 Share Posted November 19, 2009 it will match anything out that is in the single quotes, currently it only for the rvo_model but you can change the code accordingly to suite your needs Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960902 Share on other sites More sharing options...
mbk Posted November 19, 2009 Author Share Posted November 19, 2009 Thanks Rajiv, However, it is the code being assigned to $str that can change all the time. So one time it may be <script type="text/javascript"> rvo_retailer_ref='msc'; rvo_manufacturer='Brother'; rvo_model='MFC5860CNU1'; rvo_format='image_horizontal310x70'; </script> and another time it may be <script type="text/javascript"> rvo_retailer_ref='msc'; rvo_manufacturer='blahblah'; rvo_model='blahblah'; rvo_format='image_horizontal310x70'; </script> I just need to work out how to correctly assign to $str without hard coding it. Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960908 Share on other sites More sharing options...
rajivgonsalves Posted November 19, 2009 Share Posted November 19, 2009 Yep, depends on where your getting the source from, if you tell me that I could assist you further Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960911 Share on other sites More sharing options...
cags Posted November 19, 2009 Share Posted November 19, 2009 Something along the lines of file_get_contents is normally used for grabbing the contents of a site. Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960913 Share on other sites More sharing options...
mbk Posted November 19, 2009 Author Share Posted November 19, 2009 Rajiv, Cags thanks for your help, Have now got:- <?php $str = $input preg_match_all("#rvo_model='(.*?)';#", $str, $matches); print_r($matches[1]); ?> Only thing was, using preg_match_all and print_r I get Array ( [0] => MFC5860CNU1 ) I only want the MFC5860CNU1. The only way I have found round this is to change to a preg_match and use echo instead of print_r Whats the main difference between preg_match and preg_match_all? Thanks guys for the help. Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960916 Share on other sites More sharing options...
cags Posted November 19, 2009 Share Posted November 19, 2009 The difference is preg_match will return only one value, whereas preg_match_all will return an array of all values. Since your string only occurs once you get only the one value in your array. You can access it using $matches[0][0], but since your string only occurs once you might aswell just use preg_match. Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960917 Share on other sites More sharing options...
mbk Posted November 19, 2009 Author Share Posted November 19, 2009 Thanks for that cags. Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960919 Share on other sites More sharing options...
cags Posted November 19, 2009 Share Posted November 19, 2009 No problem. Please note, if you consider the topic solved theres a 'Topic Solved' button at the bottom left of any thread you started. Quote Link to comment https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/#findComment-960930 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.