Jump to content

[SOLVED] Detecting if a string ends with another


jordanwb

Recommended Posts

What I want to do is find out if a file of a given file path ends with "Theme.php". Example:

 

./themes/FiveAreaTheme/FiveAreaTheme.php returns true

./themes/FiveAreaTheme/CssBuilder.php returns false

 

I know I'd do something along the lines of this:

 


<?php

$file_path = "/foo/bar";

if (eregi ("Theme.php", $file_path))
{
do_stuff ();
}

?>

Link to comment
Share on other sites

One other thing... it may be coincidence that your file name has a dot symbol (.) in it, but that is in fact a regex special character for "any character."

 

Therefore, your regex test as is will return true for FiveAreaTheme.php as well as FiveAreaThemeXphp or FiveAreaTheme!php.  To escape the character, put a backslash before it:

 

eregi("Theme\.php$",$file_path)

 

Link to comment
Share on other sites

I would consider using preg instead of ereg. As of PHP 6, ereg (well, POSIX in general) will no longer be supported. Here is an exerpt from a page on IBM's site:

 

http://www.ibm.com/developerworks/opensource/library/os-php-future/?ca=dgr-lnxw01PHP-Future

 

"The ereg extension, which supports Portable Operating System Interface (POSIX) regular expressions, is being removed from core PHP support. If you are using any of the POSIX regex functions, this change will affect you unless you include the ereg functionality. If you're using POSIX regex today, consider taking the time to update your regex functions to use the Perl-Compatible Regular Expression (PCRE) functions because they give you more features and perform better. "

 

Basicaly, if you still have ereg stuff in your webpages and your hosting provider updates to PHP6, you'll be in for a little surprise. Granted, I think many hosting providers would give a grace period prior to fully switching over.. but some may not..either way, make your site PHP6 proof as soon as possible so that there is no issues once servers everywhere adopt it.

 

Cheers,

 

NRG

Link to comment
Share on other sites

All right so would what I use instead of ereg?

 

I saw a bit on that page about GD libraries. Is that the image libraries that do stuff like resizing?

 

GD is a graphics library.. this has nothing to do with regular expressions.. you should use preg (which is part of PCRE.. which stands for Perl Compatible Regular Expressions).

Scroll down the page in the link until you see the sub heading EREG..

 

So instead of using ereg such as in your initial example:

 

$file_path = "/foo/bar";
if (eregi ("Theme.php", $file_path))
{
do_stuff ();
}

 

You would use PCRE instead:

$file_path = "/foo/bar";
if (preg_match('#Theme\.php#i', $file_path))
{
do_stuff ();
}

 

You can read up on PCRE here:

http://us3.php.net/manual/en/book.pcre.php

 

You can read up on preg_match here:

http://us3.php.net/manual/en/function.preg-match.php

 

The preg setup is different than ereg (needing delimiters for example), but trust me, once you get the hang of preg (PCRE in general..), you won't look back.

 

This link can help you get started as well:

http://www.regular-expressions.info/index.html

 

Hope that helps.

 

Cheers,

 

NRG

 

 

 

 

Link to comment
Share on other sites

You would use PCRE instead:

$file_path = "/foo/bar";
if (preg_match('#Theme\.php#i', $file_path))
{
do_stuff ();
}

 

Ok Thanks. I have three other eregi patterns that I would have to convert for preg and I don't have a clue where to start

Link to comment
Share on other sites

I would start by examining those extra links at the bottom of my last post ;)

But just to give you rough guidance..

 

ereg(), eregi() = preg_match()
ereg_replace(), ereg_replacei() = preg_replace()

 

Just start familiarizing yourself with preg_match and preg_replace functionality. Learn about the fact that you need delimiters inside your pattern like so:

preg_match('# pattern #' , ' replace ', string); // the # characters are the delimiters.. they can be anything so long as it matches.. '% ... %' for example..

 

Just read up on it and use some of the examples found in the php.net manual links.. you'll get it soon enough..then you'll realise how much better preg is vs ereg.

Link to comment
Share on other sites

I found one regex for checking an email address:

 

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$

 

Is that for ereg or preg_match or does it matter? I also noticed that that regex won't work for .ca based email addresses.  ::)

Link to comment
Share on other sites

I found one regex for checking an email address:

 

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$

 

Is that for ereg or preg_match or does it matter? I also noticed that that regex won't work for .ca based email addresses.  ::)

 

My advise is to not use ereg (support for this will be dropped as of PHP 6) So I would stick with preg.. its more robust anyways. As for your pattern there, the reason it doesn't support .ca is that the ca portion is not included in final aspect of the pattern.. try:

 

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|ca|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$

 

Notice the ca right after the com? But a word of caution about email addresses.. if your expression is too harsh (too strict / rigid), then you might be missing out on some emails..  One code snippet I stumbled upon doesn't use any PCRE (at least, on the surface, it doesn't seem to..perhaps internally it does.. not sure).. here is what I recommend:

 

function validemail($email){
   $checkEmail = filter_var(filter_var($email, FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL);
   if($checkEmail === false){
      return false; // houtson, we have a problem...
   } else {
      return true; // houston, all systems go!
   }
}

 

The idea here is simple:

Say your email is in a variable called $_POST['email']... you pass this into the function, which in turn sanitises and validates using PHP's own built in functionalities. But this validation system is not strict.. it is much more 'relaxed'.. so yes, emails can contain formats that are not quite right.. but it is not too loose.. there are 'reasonable' limits. So it is kind of a not too strict, not too loose kind of thing (from what i read, this is the more recommended method.. that way, it lets in some perfectly good emails that would otherwise be blocked...case in point.. your pattern does not contain '.co.uk'.. so what if someone with that at the end of their email address tried to send you an email?) The system I showed allows for it.

 

Just a suggestion.

Link to comment
Share on other sites


$pattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|ca|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$";
$email = "foo@bar.com";

print preg_match ($pattern, $email) ? "True" : "False";

 

Output:

 

Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /var/www/localhost/htdocs/scm/index.php on line 24

False

 

I changed the $ at the end to a ^ and it says false.

Link to comment
Share on other sites

Do you need your pattern to be that specific?

 

Well if it would detect valid emails apart then I don't see why not.

 

PREG requires delimiters.

 

So it doesn't matter what I use a delimiter as long as it's not already used in the pattern and it has to be at the beginning and the end.

 

So why doesn't that regex work since the delimiter isn't used anywhere else in the pattern?

 


<?php

$pattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|ca|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)^";
$email = "alarm.for.cobra.11.nitro@rocks.com";

print preg_match ($pattern, $email) ? "True" : "False";

?>

 

Output:

False

 

Could it be because of casing?

Link to comment
Share on other sites

Your delimiter should typically be a character that is not a metacharacter, e.g., /pattern/. You also need the case insensitive modifier--/pattern/i--and to restore your beginning of line and end of line anchors--/^pattern$/i. I was asking about the specific domain names because (1) it's going to cause more work; and (2) that's not all of them.

Link to comment
Share on other sites

Your delimiter should typically be a character that is not a metacharacter, e.g., /pattern/. You also need the case insensitive modifier--/pattern/i--and to restore your beginning of line and end of line anchors--/^pattern$/i.

 

Um ok.  ???

 

I was asking about the specific domain names because (1) it's going to cause more work; and (2) that's not all of them.

 

Good point.

Link to comment
Share on other sites

As effigy mentioned... you need delimiters.. this is one of the immediate (and perhaps confusing) differences between preg and ereg.. so.. your current pattern is:

 

$pattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|ca|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$";

 

At the very begninning and very end of your pattern , you must add delimiters.. Perl Compatible Regular Expressions is very flexible with regards to this.. you can have anything you want (I think.. effigy can correct me on this if I am wrong).. so the default delimiters you see most often is '/' or either side.. but I personally like '#' (this is a drasw back for commenting inside patterns, but this is another story entirely). So using my style, examine what you have with this:

 

$pattern = "#^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|ca|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$#i"; the i outside the delimiter means upper or lowercase characters...

 

Notice the # characters, one at each end of this pattern? You can use '/' as delimiters if you like.. but understand that whatever character you choose as a delimiter, if that same character is needed as part of a match inside your pattern, you must escape it with a backslash '\'.  so if I needed to use the # character as part of my pattern, I would need to do this:

 

$pattern = '# ...\# ... #'; // otherwise, without escaping, regex would confuse my # character inside as a closing delimiter...

 

Some 'safer' (read.. less likely to need escaping) alternative delimiters could be { ... } or % .... %. But no matter what.. you need delimiters.

Link to comment
Share on other sites

I know this seems slightly confusing and frustrating at first.. but trust me.. after you do a few examples with preg (with delimiters of course  ;) ), you'll see it isn't that difficult to grasp.. the trickier part about regex in general is not the syntax, but rather the rules of engagment. Some characters inside a pattern can have their meanings changed (depending on where they are located.. that is to say, inside a character class or not).. but this all comes with simple practice and a little research on web tutorials delaing with PCRE.

 

Trust me.. once you start to grasp regex, you'll wonder how you managed without it.

Link to comment
Share on other sites

It's important to understand why using ^ as a delimiter won't work.  If you're still confused about that, take a look at the first table in this regex cheat sheet:

 

http://regexlib.com/CheatSheet.aspx

 

The metacharacter (^) refers to the beginning of a string.  The metacharacter ($) is the end string (remember.. from my first response?).

 

When you search a string with a regex like "abc," it will return a match within any of the following: "abc", "aabc", "aabcasdfjaskdjf".  However, your original question stated that you wanted your string "Theme\.php" to be at the end of the string, that is, nothing should come after it.  Changing your regex string to "Theme\.php$" will find a match in "ajaskdjfa;dji2348uTheme.php" but not "Theme.php1" because the character "1" does not match the regex $ metacharacter, which says to look to see if that is the end of the string.

 

You can think of delimiters as a set of parentheses that captures all of the regex string, except rather than actual parentheses, you can use any symbol that ISN'T a metacharacter (again, refer to the cheat sheet).  The reason delimiters are used is that after the closing delimiter, you can put in flags like "i" or "g" to specify other options, like case-insensitivity and global searching.  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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