Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

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?
0
FourMat
Asked:
FourMat
  • 4
  • 4
1 Solution
 
Terry WoodsIT GuruCommented:
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.
0
 
Terry WoodsIT GuruCommented:
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

0
 
Terry WoodsIT GuruCommented:
Fixed one error in that:
$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_prefix.$source_id, "/")."/i", $tracker_array['landing_page'])) {
    $orderslist[$tracker_array['order_id']]['landing_page'] = $landing_value;
    break;
  }
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Terry WoodsIT GuruCommented:
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.
0
 
FourMatAuthor Commented:
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?
0
 
FourMatAuthor Commented:
I'm also getting the same error when I use the code without the array creation.
0
 
FourMatAuthor Commented:
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!
0
 
FourMatAuthor Commented:
Thanks for the additional help over and above the original question.  I learned a bit more today thanks to you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now