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
https://forums.phpfreaks.com/topic/182130-solved-regex-help-needed-pls/
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]);
?>

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.