Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > preg_match
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 20th May 03:14
php
External User
 
Posts: 1
Default preg_match



Hi,

I don’t understand regular expressions at all – I will make an effort to
learn about them one day, but I need a solution today. I want to use the
__autoload function, but not for all my class only those that finish in
‘Controller’. So for instance,

‘ErrorController’ should load ‘errorcontroller.inc.php’
‘IndexController’ should load ‘indexcontroller.inc.php’
‘FooBar’ should NOT load anything whatsoever.

Can you help me write an __autoload function please??

Alex

No virus found in this outgoing message. Scanned by AVG Free 8.0
Checked by AVG - http://www.avg.com
Version: 8.0.175 / Virus Database: 270.8.3/1748 - Release Date: 27/10/2008
07:57
  Reply With Quote


  sponsored links


2 20th May 03:14
stuttle
External User
 
Posts: 1
Default preg_match



Have you even tried it? This is not hard, and doesn't need to use any
regular expressions at all.

We're not here to write code for you, we're here to help when you have
problems. Try it, see how far you get and send us the code if/when you
get stuck.

Unless you want to hire me to do it. My rates are pretty reasonable
for simple stuff like this.

-Stut

--
http://stut.net/
  Reply With Quote
3 20th May 03:14
php
External User
 
Posts: 1
Default preg_match


Problem solved:
function __autoload($c) {
$m = array();
preg_match('/(?:^[A-Z][a-z]+)Controller/', $c, $m);
if (count($m)) {
require_once(realpath(FS_CONTROLLER . '/' . strtolower($m[0]) .
'.inc.php'));
}
}

However (perhaps a more appropriate question), do you think there is an
easier/better way to do this??

Alex

No virus found in this outgoing message. Scanned by AVG Free 8.0
Checked by AVG - http://www.avg.com
Version: 8.0.175 / Virus Database: 270.8.3/1748 - Release Date: 27/10/2008
07:57
  Reply With Quote
4 20th May 03:14
stuttle
External User
 
Posts: 1
Default preg_match


See, that wasn't so hard was it!!

Personally I'd have gone with something more like this...

function __autoload($class)
{
if (substr($class, -10) == 'Controller')
{
// No need for realpath here, it's not doing anything useful
// The _once is probably redundant too, but it may be required
// depending on how your code is arranged.
require FS_CONTROLLER.'/'.strtolower($class).'.inc.php';
}
}

....but there's always more than one solution.

-Stut

--
http://stut.net/
  Reply With Quote
5 20th May 03:14
eric.butera
External User
 
Posts: 1
Default preg_match


I'd just like to add spl autoload is friendlier since __autoload can
only be defined once.

http://us.php.net/manual/en/function.spl-autoload-register.php
  Reply With Quote
6 20th May 03:14
stuttle
External User
 
Posts: 1
Default preg_match


Please keep the conversation on the list!

If you're going to use regular expressions you need to read the manual
to understand how they work. There's a whole section of the manual
that covers this - go read it to understand what the ^ means!

-Stut

--
http://stut.net/
  Reply With Quote
7 20th May 03:14
tmboyd1
External User
 
Posts: 1
Default preg_match


in


http://www.regular-expressions.info

Awesome website. You should be a veritable RegEx guru after reading that
site for one day. (It'll also tell you what the ^ and $ means, as well
as that (? group).


Todd Boyd
Web Programmer
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666