Link to home
Start Free TrialLog in
Avatar of FourMat
FourMat

asked on

PHP preg_match syntax questions

Hello,
I am trying bring an older php script up to date and replace the eregi() function with preg_match().  I'm running into a small issue with the syntax required.  

This is the current code:
$source = 'utm_source='; 
if (eregi($source . 'Googlead', $tracker_array['landing_page']))    $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="blue"><b>Google Adwords</b></font>';
elseif (eregi($source . 'googlead', $tracker_array['landing_page']))    $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="blue"><b>googlead</b></font>';
 elseif (eregi($source . 'shopping', $tracker_array['landing_page']))    $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="red"><b>shopping</b></font>';
elseif (eregi($source . 'yahoo', $tracker_array['landing_page']))       $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="purple"><b>yahooad</b></font>';
elseif (eregi($source . 'shopzilla', $tracker_array['landing_page']))   $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="orange"><b>shopzilla</b></font>';
elseif (eregi($source . 'twitter', $tracker_array['landing_page']))   $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="orange"><b>Twitter</b></font>';
 elseif (eregi($source . 'facebook', $tracker_array['landing_page']))   $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="orange"><b>FaceBook</b></font>';
elseif (eregi($source . 'googprod', $tracker_array['landing_page']))   $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="red"><b>Google Products</b></font>';
elseif (eregi($source . 'newsletter', $tracker_array['landing_page']))   $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="red"><b>CC Newsletter</b></font>';
elseif (eregi($source . 'ccpromo', $tracker_array['landing_page']))   $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="red"><b>CC Sales Promo</b></font>';

Open in new window


My thought was to escape the string with a /string/i but the string is actually being assembled inside of the function.  So I tried to escape the $source string as well like this

$string = "/utm_source/i";

Open in new window

 
When I tried that I got this warning:

Warning: preg_match() [function.preg-match]: Unknown modifier '/'

What should be the proper syntax for this scenario?
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Changing this:
if (eregi($source . 'Googlead', $tracker_array['landing_page']))    $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="blue"><b>Google Adwords</b></font>';
to this:
if (preg_match("/",preg_quote($source.'Googlead', "/")."/i", $tracker_array['landing_page']))    $orderslist[$tracker_array['order_id']]['landing_page'] = '<font color="blue"><b>Google Adwords</b></font>';

should work for you.
This would be neater:
$source_prefix = 'utm_source='; 
$landing_map = array(
'Googlead'=>'<font color="blue"><b>Google Adwords</b></font>'
'googlead'=>'<font color="blue"><b>googlead</b></font>'
'shopping'=>'<font color="red"><b>shopping</b></font>'
'yahoo'=>'<font color="purple"><b>yahooad</b></font>'
'shopzilla'=>'<font color="orange"><b>shopzilla</b></font>'
'twitter'=>'<font color="orange"><b>Twitter</b></font>'
'facebook'=>'<font color="orange"><b>FaceBook</b></font>'
'googprod'=>'<font color="red"><b>Google Products</b></font>'
'newsletter'=>'<font color="red"><b>CC Newsletter</b></font>'
'ccpromo'=>'<font color="red"><b>CC Sales Promo</b></font>'
);

foreach ($landing_map as $source_id=>$landing_value) {
  if (preg_match("/",preg_quote($source.$source_id, "/")."/i", $tracker_array['landing_page'])) {
    $orderslist[$tracker_array['order_id']]['landing_page'] = $landing_value;
    break;
  }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note that because you've been using a case-insensitive match (eregi instead of ereg), you would never get a match on 'googlead' as it would always be matched by 'Googlead' first. My code has replicated that issue, since it's up to you to determine the best way to resolve it - feel free to provide more detail if you want advice on it.
Avatar of FourMat
FourMat

ASKER

Hi Terry, thanks a ton for the help.  I'm having an issue though.  I have tried your array coding, and amrunning into an error:

Warning: preg_match() [function.preg-match]: No ending delimiter '/' found in /salestracker.php on line 255

Where line 255 is this:

if (preg_match("/",preg_quote($source_prefix.$source_id, "/")."/i", $tracker_array['landing_page'])) {

Open in new window



I'm confused as to what it means by ending delimiter.  It appears that there is one there, but I'm wondering if using preg_quote is adding an open delimiter, but nothing is there to close?
Avatar of FourMat

ASKER

I'm also getting the same error when I use the code without the array creation.
Avatar of FourMat

ASKER

Ah, I think I figured it out.  The comma should be a period after the first "/" in the preg_match statement.

Everything seems to be working properly now.

on the issue of the case insensitivity:  I had realized that after I started working on updating the script, but thanks for the advice!
Avatar of FourMat

ASKER

Thanks for the additional help over and above the original question.  I learned a bit more today thanks to you.