Jump to content

[SOLVED] Regex Help Needed pls


mbk

Recommended Posts

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

Link to comment
Share on other sites

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]);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.