jedi_ralf 0 Newbie Poster

If explode uses a string and split uses regex then what does preg_split use? What is the difference between split and preg_split? Just curious.

"explode() takes a delimiter to split by ... split() takes a regular expression"
"documentation says that preg_split() is faster than split()"
http://blog.brianhartsock.com/2007/06/11/php-explode-vs-split/

"preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine."
- http://uk3.php.net/split

Some speed stats: http://forums.codewalkers.com/php-coding-7/split-vs-preg-split-vs-explode-71423.html

So in answer to your question, preg_split uses regex like split. However, I don't know what the exact difference is.

jedi_ralf 0 Newbie Poster

hi my site is not working properly with ie7....firefox and opera are fine......

Elaborate on "not working". That means nothing. What's not working?
As a side note, IE6 is not compatible with your site as it cannot render png images.

jedi_ralf 0 Newbie Poster

Anyway, the Min-height:600 worked for most of the pages, but there are others that are much longer. So those didn't take.

I think the problem is that you have 3 columns of data; the menu (left_nav), the body (right_left_1) and the contact (right_left_2) section.
Because these sections are using "float: left;", they are therefore not filling the container div "body_2". Therefore as this has no height (unless you've specified it as was suggested), there is no background colour to cover up the gaps.

Use this tutorial to make each "column" the same height. http://www.ejeliot.com/blog/61

You will also need to make the body (right_left_1) or contact (right_left_2) section slightly wider to take up the space that is peeking through between them (about 3px worth).

It will require some tinkering and some use of a standards compliant browser to get it looking good for everyone

jedi_ralf 0 Newbie Poster

I need to have this nav know exactly which category the page is currently viewing so I can highlight the item in the subnav so the user knows by looking at the subnav, which page the are on.

Let me see if I understand.
The cat number will be held in the URL, right? - $_GET
And you want the item where $cid == $_GET to be highlighted?

So is this vaguely what you mean?
(I put the <div> into an echo; makes it easier to add in variables)

// fetch the next row of results set until there are no more left
  while ($showroomlink = mysql_fetch_array($resultshowroomlink)) {
    // store results in variables
    $cid = $showroomlink['cid'];
    $cname = $showroomlink['cname'];
    echo "              <div class=\"link\"";
    if ($cid == $_GET['cat']) echo " style=\"####INSERT HIGHLIGHT STYLE####\"";
    echo ">";
    echo "                <span class=\"item\" id=\"main\"><a href=\"category.php?cat=$cid\">".stripslashes($cname)."&nbsp; •</a></span>\n";
    echo "              </div>\n";
  // end loop
  }
jedi_ralf 0 Newbie Poster

Just to add. I'm using CSS to style the nav using Divs and Spans. It's just a matter of calling the selected category from the loop to highlight it as selected.

i.e. on the page category.php?cat=3 . How can I highlight cat 3 in the nav to show that it's the page being viewed.

You can do it in PHP or you can do it with CSS only.
PHP - http://www.alistapart.com/articles/keepingcurrent/#
CSS - http://hicksdesign.co.uk/journal/highlighting-current-page-with-css

jedi_ralf 0 Newbie Poster

Thanks both of you, first off. Now I have added the $dir variable as a global, and when designating the path as ".", I get the thumbs to display. When I change it to G:\xampp\xampp\htdocs\images, I get:
Warning: opendir(G: mpp mpp\htdocs\images) failed to open
The issue here looks like an escape character issue, yet only applied to the fist two directories. In python, \ is the escape character (ie: G:\\xampp\\xampp\\htdocs\\images), so I'm not sure the issue here. Thanks again.

Firstly, should the slashes not be forward slashes, rather than back slashes?
"G:/xampp/xampp/htdocs/images/"
I'm fairly sure it's only windows that does it this way - "\".
Secondly, have you tried a relative path instead of an absolute one?
ie. If your script is in "htdocs", instead of giving the address as "G:/xampp/xampp/htdocs/images/" all it would need to be is "images/"
Just some thoughts...

jedi_ralf 0 Newbie Poster
//adjust mysql query accordingly 
$result=mysql_query("SELECT * FROM `users` WHERE `username`='".$_POST['username']."' AND `password`='".$_POST['password']."'");

This doesn't seem very safe...

//adjust mysql query accordingly 
$result=mysql_query("SELECT * FROM `users` WHERE `username`='".mysql_real_escape_string($_POST['username'])."' AND `password`='".mysql_real_escape_string($_POST['password'])."'");

You need to use mysql_real_escape_string() to stop injection attacks.
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php

jedi_ralf 0 Newbie Poster

Hello,
I am new to php and enjoy the fact that it is very similar to languages that I'm much stronger with (mainly C++ and Java). While working through the w3 school's tutorials I noticed a foreach() loop. What loop in C++/Java would this be similar to?

It's similar to Java's enhanced for loop when using an iterator over a collection of objects. It's also called a foreach loop although uses the syntax "for".
Here's a link about Java's enhanced for loop: http://www.developer.com/java/other/article.php/3343771

jedi_ralf 0 Newbie Poster

hi, i want to create application for like
1> real estate & Property or
2> landlord and property ...
there is any opensource code to create a this type of application??
or demo links..

This looks like a question for google...
http://lmgtfy.com/?q=open+source+real+estate+Property+landlord+application

A few links I quickly found:
http://www.open-realty.org/
http://www.open-realty.co.uk/
http://www.rentomatic.com/

jedi_ralf 0 Newbie Poster

I have IPB 2.3.5. I need to decrypt (or encrypt) the passwords from the database so I can compare them to user input from another page.

It's unlikely you'll decrypt them; they're hashed.

So, how does anyone happen to know how IPB encrypts their passwords? (Or how to decrypt them?)

A quick google search for "IPB password hashing" threw up this link:
http://www.neowin.net/forum/index.php?showtopic=317007

It seems to be talking about what you want to do;
"to generate the passwords, ipb generates an md5 hash of it, "salts" it, and then re-hashes the salt. the salt i beleive is stored in ibf_members_extra, but i am not sure. note: it is unique for each user"

jedi_ralf 0 Newbie Poster

For those who are interested, I found out a regular expression that can match the pattern I was looking for.

Old Expression:

/Name: (?<name>\w+), Age: (?<age>\d+),.*?Location: (?<location>.*?)/is

New Expression:

/Name: (?<name>\w+), Age: (?<age>\d+), (?:(?:(?!Name:\s).)*Location: (?<location>[^\n]*)\n)?/is

The new section can be show more generically:

(?:(?:(?![STRING NOT ALLOWED]).)*[BOUNDARY BEFORE](?<[GROUP NAME]>.*?)[BOUNDARY AFTER])?

where you replace (Including the [ and ]):
[STRING NOT ALLOWED] = String you don't want allowed through
[BOUNDARY BEFORE]    = The text or characters just before the information you want
[BOUNDARY AFTER]     = The text or characters just after the information you want

For those who wish to know how the expression works, read the details between the dashed lines.
-------------------------------------------------------------------------------------------------
The new section can be broken into 3 main sections:

1. (?:
2. (?:(?!Name:\s).)*
3. Location: (?<location>[^\n]*)\n
4. )?

1. and 4. are the wrapper for the non-capturing group (http://www.regular-expressions.info/named.html) which can occur 0 or 1 times (ie. optional)
2. is a non-capturing group containing a negative look ahead, checking that the string Name is not present. This group occurs 0 or more times as you want to check all the characters before reaching Location. - **Note 1**
3. Matches 0 to many characters which are not a new line between "Location: " and "\n" (new line character) and stores them in named group "location" - **Note 2**

**Note 1**
This is a greedy search and will only return the last match before "Location: ".
Therefore, had there been …

jedi_ralf 0 Newbie Poster

If you are saying that you want another array or field then the following should allow for the location array providing it remains called "location:" (without the quotes)
...
Hope that helps answer it.

Had a mess about with the code. There was one bug

unset($properties[$joinid]);

which would always run. This was a problem when there weren't commas in the Details section, as it would then unset array element 3, therefore deleting the Location. eg:

Name: Julian, Age: 34, Details: Has too many CDs, Location: Paris

However, thank you very much for the code. Here is my final version:

<?
$string = <<<EOF
Name: Bob, Age: 20, Details: Likes chocolate, sweets, 
cake and fishfingers, 
Location: London, New York

Name: Henry, Age: 65, Details: Hate rain,
Loves sunshine

Name: Julian, Age: 34, Details: Has too many CDs, Location: Paris
EOF;

$allResults = array();
$i = -1;
$eachEntry = explode("Name: ", $string);
foreach($eachEntry as $entry) {

   $entry="Name: ".$entry;
   $joinid = 3;
   $join = true;
   $properties = explode(",", $entry);

   while(isset($properties[$joinid])) {
      if (!preg_match('/\s*Location\:/i',$properties[$joinid]) && $join) {
         $properties[2].=', '.trim($properties[$joinid]);
         unset($properties[$joinid]);
      } else {
         if ($joinid != 3) {
            if ($join) {
               $join = false;
               $properties[3] .= trim($properties[$joinid]);
            } else {
               $properties[3] .= ', '.trim($properties[$joinid]);
            }
            unset($properties[$joinid]); 
         }
      }     
      $joinid++;
   }

   $result = array();
   foreach($properties as $property) {
      $split = explode(":", $property);
      $result[trim($split[0])] = $split[1];
   }

   if ($i != -1)
      $allResults[$i] = $result;
   $i++;
}

echo "<xmp>";
print_r($allResults);
echo "</xmp>";
?>

And gives the output

Array
(
    [0] => Array
        (
            [Name] => …
jedi_ralf 0 Newbie Poster

Well the following code should solve that for you:

<?
$string =  'Name: Bob, Age: 20, Details: Likes chocolate, cake and fishfingers';
  $properties = explode(",", $string);
  $result = array();
  $joinid=3;
  while(isset($properties[$joinid])) {
  $properties[2].=','.$properties[$joinid];
  unset($properties[$joinid]);
  $joinid+=1;
  }
  foreach($properties as $property) {
    $split = explode(":", $property);
    $result[$split[0]] = $split[1];
  }
  print_r($result);
?>

Thanks for that.
If I've dry run your code properly, a problem I can see is that by appending everything after element 3 onto element 3, if there was a location, it will also get appended onto element 3.
Then when you explode by ":" it would be element 2, which would not be picked up by the results.

I had hoped the code could be kept simple by using a regular expression as Details is an unknown input and could potentially contain anything.

Thanks for your replies.
Ralf

jedi_ralf 0 Newbie Poster

There's no need to use a regular expression for that

$string =  'Name: Bob, Age: 20, Details: Likes chocolate';
  $properties = explode(",", $string);
  $result = array();
  foreach($properties as $property) {
    $split = explode(":", $property);
    $result[$split[0]] = $split[1];
  }
  print_r($result);
  /*
    Array
      (
        [Name] => ' Bob',
        [Age]  => ' 20',
        [Details] => ' Likes chocolate'
      )
  */

That's a good solution, thanks!
The only problem is if there were commas in the details section, eg:
"Likes chocolate, cake and fishfingers". Any ideas?

Also, out of interest, is explode more efficient than regular expressions? The examples I gave were only a small section of a larger page.

Thanks again,
Ralf

jedi_ralf 0 Newbie Poster

Hello,
I have not had a huge amount of experience with regular expressions.
Any help you can give is greatly appreciated.

I have an input file like this:

Name: Bob, Age: 20, Details: Likes chocolate
Hates lettuce, Location: London

Name: James, Age: 42, Details: Sometimes goes swimming, Location: New York

Name: Jenny, Age: 15, Details: Eats out every night
Dislikes cats
Loves Dogs, Location: Tokyo

Name: Henry, Age: 65, Details: Hate rain
Loves sunshine

Name: Julian, Age: 34, Details: Has too many CDs, Location: Paris

I want to match some details using the regular expression and preg_match_all:

/Name: (?<name>\w+), Age: (?<age>\d+),.*?Location: (?<location>.*?)/is

Where it holds the values of name, age and location, over multiple lines and being case insensitive.

However, the above regular expression will match the below as one entity because "Henry" does not have a location.

Name: Henry, Age: 65, Details: Hate rain
Loves sunshine

Name: Julian, Age: 34, Details: Has too many CDs, Location: Paris

Is is possible to have it so that the match ".*?" cannot contain the string "Name" (which would then prevent it from carrying on to into another person's entry).
eg:

/Name: (?<name>\w+), Age: (?<age>\d+),.*?[^Name]Location: (?<location>.*?)/is

However, I realise this is not correct as [^Name] matches N, a, m or e
I know it's possible for a single character, but how about a string?

Thank you for any help you can give,
Ralf

jedi_ralf 0 Newbie Poster

Did you test your code?
Do you get the expected result?

Gave it a go and got the results I was looking for. Took a bit of fiddling, but it works ^_^

#!/bin/gawk -f
{
   setTags[1,1] = "<img "
   setTags[1,2] = "src"
   setTags[1,3] = ">"

   setTags[2,1] = "<a "
   setTags[2,2] = "href"
   setTags[2,3] = ">"

   for ( j = 1; j < 3; j++ ) {
      expression = setTags[j,1]setTags[j,2]"[^>]+[.]*"setTags[j,3]
      gsub(expression, "")
      if (j==2) 
         gsub("</a>", "")
   }
}

Runs through each line of the file and removes images and links.
The ending of the link (</a>) has to be removed separately because of the "[^>]" in the regular expression (which prevents it overrunning). However, this also has the added advantage of leaving in the text or image which was wrapped in the link tags.

Thank you again for your help.
I have learnt alot.

jedi_ralf 0 Newbie Poster
awk 'END { 
print "#links:", _
  for (i=1;i<=_;i++)
    print la[i]
print "#images:", __
  for (i=1;i<=__;i++)
    print ia[i] }
{ r = $0; lnk = "a href="; img = "img"; tag = "<("lnk"|"img")([^>]*)>"
while (match(r = substr(r, RSTART+RLENGTH), tag, t)) {
  if (substr(r, RSTART, RLENGTH) ~ "^<"lnk)
    la[++_] = t[2]
  else
    ia[++__] = t[2]  
  }
}'  html

Wow, that's brilliant, thank you! You have solved all my problems!

I've ended up using your example of the while loop with a match in it (before I had been splitting by the tag and then using a for loop to loop through each instance of the tag). It now looks so much neater and clearer.
Also, using the or (|) between different tags has worked brilliantly, without the need for multiple loops for each tag.

I see that in my first post, although explaining my problem, I did not justify it well. As well as finding the tag in the line, I also needed to find the entire <a href=".."> or <img src="..." /> and replace it.
My final regular expression ended up being

/(<a|<img)[^>]+(href|src)=http:\/\/[^>]+[.]*>/

which can be used to replace the front tag of the link or an entire image tag, regardless of (x)html standard and of any other atributes which may be present (class, id, target, etc)

Finally, it's good to know that variables can be used, so long as you assign the whole regular expression to another variable before use. In this case …

jedi_ralf 0 Newbie Poster

If you have GNU Awk:

awk 'END { 
print "#links:", _
  for (i=1;i<=_;i++)
    print la[i]
print "#images:", __
  for (i=1;i<=__;i++)
    print ia[i] }
{ r = $0
while (match(r = substr(r, RSTART+RLENGTH), /<a href=([^>]*)>/, t)) 
  la[++_] = t[1] 
}
{ r = $0
while (match(r = substr(r, RSTART+RLENGTH), /<img([^>]*)>/, t))
  ia[++__] = t[1] 
}'  html

Thank you for the code. There's some advanced stuff in there I can learn from.
However, the problem still remains that there will be code redundancy because I am still using two while loops, one for links and one for images. Is there no way I can replace the "<img" and "<a" tags with a variable and have only one while loop?
Thanks for your help so far!

jedi_ralf 0 Newbie Poster

Thanks for your reply.

Could you post a sample html input and the desired output?

Sure. I don't quite see how it's relevant to the question though.
Anyway, the html can be any webpage. All I'm interested in are the link and image tags.
eg:

<a href="http://www.google.com">Google</a> - <img src="http://www.google.com/intl/en_ALL/images/logo.gif" />

In essence, this is a small part of my larger program. I will also be assigning the assigment operator (href or src) and the end tag (<a/> or >) to the 2d array.
These will be used to retrieve the address within the tag.

So far I have not planned an output. I simply wish to get an array of all link and image addresses within the webpage. Once I have that ouput will be a simple process of running through the array, printing all, or counting duplicated, etc.

Do you know if it's possible to have variables in a regular expression?

jedi_ralf 0 Newbie Poster

I am new to awk, so please excuse any mistakes.
I was hoping someone would be able to tell me if it's possible to include variables in a regular expression, and if so, how.

The code I currently have is the find the beginning of link (<a) and image (<img) tags in an html page.
Instead of duplicating this same code for each tag (ie, hard code each value in), I was hoping to be able to use a for() loop to loop over the same expressions, simply changing the variable each loop.

The code I have:

setTags[1,1] = "<a"
setTags[2,1] = "<img"

for ( j = 1; j < 3; j++ ) {
         TAG = setTags[start,1]
         split($0,removeFront,/[>]?[.*]?TAG/)
}

This loops through twice, once looking to split by a link tag, and another to split by the image tag. This will tell me how many links and how many images are on each line.
However, I am unable to get the TAG variable (only uppercase to make it stand out) interpreted. Depending on what I do, it will look for the letters T, A and G, or it simply won't work.

Any assistance you can give would be greatly appreciated.
Thanks,
Ralf