homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

script optimization

 3:19 am on Mar 20, 2013 (gmt 0)

I use a script where I pull data from search engines. I wonder about the code below. In some cases I have over 200 entries for various sites. As I see it right now, it goes line by line until it hits or does not find.
Can this be optimized to be faster and lighter on the server?

$site_query = array(
"google" => "q:01",
"yahoo" => "p:02",
"bing" => "q:03",
"live" => "q:04",
"aol" => "q:05",
and so on




 10:52 am on Mar 22, 2013 (gmt 0)

That's just an array. Are you saying you are looping through it? If so, and you are merely looking for a certain entry, then I would test for the index being set instead and yes, it will be much faster. I'm not sure how or what format your data is arriving but I'll assign it to a variable here first. But the main thing is to use isset():
$searchEngine = 'live'; 
if (isset($site_query[$searchEngine])) {
// found
$q = $site_query[$searchEngine]; // $q = q:04
} else {
// not found


 4:16 pm on Mar 22, 2013 (gmt 0)


I believe that the array was built to make it faster then when using IF approach.

This is what happens after the array:

foreach ($_GET as $key => $value)
if(! $_GET[$key]) {
$_GET['var1'] = $key;

where "var1" is one of my variables.

After that, there are several lines of code to get the query string:

$v1 = $_GET['var1']; //my variable attached to incoming ad URL

list($url, $querystring) = split("\?", $_SERVER['HTTP_REFERER']);
$url = preg_replace("|http://([^\/]+)/.*|", "\\1", $url);

$referer = $url;
foreach ($site_ques as $key => $value)
if(preg_match("/.*\.$key\..*/", $url)||preg_match("/^$key\..*/", $url))
list($ques, $referer) = split(":", $value);
$v3 = $referer;

if (isset($ques) && preg_match("/^([^\&]+\&)*$ques=([^\&]*)(\&[^\&]+)*$/", $querystring))
$v2 = preg_replace("/^([^\&]+\&)*$ques=([^\&]*)(\&[^\&]+)*$/", "\\2", $querystring);
} else {
$v2 = 'FindQuery';

I just wondered if there was a faster way than having a list if search engines in an array. This was made for me for my sites and this is the stripped version for direct linking.

Thank you

[edited by: engine at 4:27 pm (utc) on Mar 22, 2013]
[edit reason] Disable graphic smile faces for this post [/edit]


 1:25 am on Mar 24, 2013 (gmt 0)

But with that there will be no number assigned to each source search engine. That's also what the script does. Instead of passing through the names of referring sites, I replace it with the code (plain number).



 3:57 pm on Mar 28, 2013 (gmt 0)

This part is where I would focus first, if at all possible ...
foreach ($site_ques as $key => $value) { 
if(preg_match("/.*\.$key\..*/", $url)||preg_match("/^$key\..*/", $url)) {
list($ques, $referer) = split(":", $value);

I was recommending going for the direct hit using isset() rather than loop through the entire array. However, I see now that you have a regular expression match going on in there against the referrer to figure out which SE it is. In order to use the isset() approach you would need just the key index value (google|yahoo|bing|etc.) out of the referrer first. Which presents a whole new task. Perhaps your loop *is* your best option.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved