CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Messing? PHP Code but it works...  (Read 3761 times)

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Messing? PHP Code but it works...
« Reply #15 on: 9 Jan 2021, 09:12:55 am »
Oh I see the problem, your regex can return integer 0 as a no-match in addition to false. As such we can simplify this further as we can switch from strict to loose.

Code: [Select]
<?php
echo '<div>1: 'vrm_Validate("TEST") ? "match" "no match"'</div>';

echo 
'<hr>';

echo 
'<div>2: 'vrm_Validate("a1abc") ? "match" "no match"'</div>';


function 
vrm_Validate($vrm) {

$vrm strtolower($vrm);

echo '<div>VRM: '$vrm'</div>';

$regexs = [

'/^([a-z]{3})\s?([0-9]{1,3}[a-z]{1})$/'
'/^([a-z]{1}[0-9]{1,3})\s?([a-z]{3})$/',
'/^([a-z]{2}[0-9]{2})\s?([a-z]{3})$/',
'/^([a-z]{3})\s?([0-9]{3})$/',
'/^([a-z]{3})\s?([0-9]{4})$/',
'/^([a-z]{1})\s?([0-9]{1})$/'

];

foreach ($regexs as $regex) {
echo '<br>rx: '$regex' ';
if (preg_match($regex$vrm)) return true;
}

return false;

}

that works.

THANK YOU!!!

Which way is best?

So was it my BAD code?

Should I just give up?
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

John_Betong

  • Full Member
  • ***
  • Posts: 218
  • Karma: +24/-1
    • The Fastest Joke Site On The Web
Re: Messing? PHP Code but it works...
« Reply #16 on: 10 Jan 2021, 07:55:21 am »
I prefer using PHP functions to extract strings rather than REGEX. They may be a little more verbose but far easier to modify and update...


so I was procrastinating and making good with my time because I want to use some of the script on another project:


https://this-is-a-test-to-see-if-it-works.tk/codingforum/GrumpyYoungMan/registration-plates/
« Last Edit: 10 Jan 2021, 08:59:19 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Messing? PHP Code but it works...
« Reply #17 on: 10 Jan 2021, 09:12:54 am »
I prefer using PHP functions to extract strings rather than REGEX. They may be a little more verbose but far easier to modify and update...


so I was procrastinating and making good with my time because I want to use some of the script on another project:


https://this-is-a-test-to-see-if-it-works.tk/codingforum/GrumpyYoungMan/registration-plates/
Thanks!!

So is it down to personally taste? Or is regex a thing of the past now?
« Last Edit: 10 Jan 2021, 09:22:24 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

John_Betong

  • Full Member
  • ***
  • Posts: 218
  • Karma: +24/-1
    • The Fastest Joke Site On The Web
Re: Messing? PHP Code but it works...
« Reply #18 on: 10 Jan 2021, 09:19:22 am »
Please try again - I was trying to change the name of the pathEdit:
Quote
So is it down to personally taste? Or is regex a thing of the past now?


I have not used REGEX very much and do not find it easy to understand at a glance. There are an abundance of PHP functions which do the same job and they can be easilt hidden away in function.

Also I have read numerous topics/threads of users trying to get REGEX to do what they want it to do and usually the posts are numerous
« Last Edit: 10 Jan 2021, 09:38:03 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Messing? PHP Code but it works...
« Reply #19 on: 10 Jan 2021, 09:23:21 am »
Yeah it worked - I did think it was me being too quick in the fingers! lol!


I have also changed my first post after your originally post!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

John_Betong

  • Full Member
  • ***
  • Posts: 218
  • Karma: +24/-1
    • The Fastest Joke Site On The Web
Re: Messing? PHP Code but it works...
« Reply #20 on: 10 Jan 2021, 09:39:03 am »
Please try again - I was trying to change the name of the path


<br>
Edit:
Quote
So is it down to personally taste? Or is regex a thing of the past now?


I have not used REGEX very much and do not find it easy to understand at a glance. There are an abundance of PHP functions which do the same job and they can be easilt hidden away in function.

Also I have read numerous topics/threads of users trying to get REGEX to do what they want it to do and usually the posts are numerous

Retired in the City of Angels where the weather suits my clothes

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Messing? PHP Code but it works...
« Reply #21 on: 10 Jan 2021, 09:39:40 am »
So is it just the normal with PHP - there is more than one way to do it, rather a practically bad way?

I will look at your code once I get back on the laptop!

Thank you for your assistance and guidance John and of course to Jason! 😎
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

mmerlinn

  • Jr. Member
  • **
  • Posts: 77
  • Karma: +9/-0
  • Nutcake
Re: Messing? PHP Code but it works...
« Reply #22 on: 10 Jan 2021, 08:19:47 pm »
As noted above, I don't know PHP.

If this were FoxPro, I would do it this way:

Code: [Select]
zvar = '     abC   2 1 def     '
zspace = ' '
znull = ''
zvar = UPPER(CHRTRAN(zvar, zspace, znull))     && Remove all spaces, Change to upper case
z = zvar

DO WHILE NOT EMPTY(z)
    zlen = LEN(z)
    IF ISDIGIT(RIGHT(z, 1))
        z = znull     && Digit found, Exit
    ELSE
        z = LEFT(z, zlen - 1)     && Digit not found, Loop
    ENDIF
ENDDO

zvar = ALLTRIM(STUFF(zvar, zlen + 1, 0, zspace))     && Insert space, Trim any trailing space

The only time this fails is if the string variable contains no numeric digits. This also assumes that the string is entered correctly, Alpha + Numeric + Maybe more alpha.

Based on what I see in John's code above, a similar solution should be available using PHP.

Not tested.
« Last Edit: 10 Jan 2021, 08:31:59 pm by mmerlinn »
The soul purr pus of a spell cheque cur is two valley date hour ignore ants.

John_Betong

  • Full Member
  • ***
  • Posts: 218
  • Karma: +24/-1
    • The Fastest Joke Site On The Web
Re: Messing? PHP Code but it works...
« Reply #23 on: 11 Jan 2021, 12:51:42 am »
@mmerlinn,
FoxPro !!!!! Reminds me of my Clipper days :)


I assumed that the minimum length of registration was six characters?

Following are the three PHP function to return "Area code", "Age identifier" and "Random letters". If the extraction fails then an "empty string" is returned. Only one function return value needs to be empty and the whole registration plate is invalid.


Code: [Select]
//=========================================
// Area code
//=========================================
function getV1
(
  string $vrm
)
: string
{
  $result = ''; // FALSE

# IS SECOND CHARACTER A NUMERIC
  // $v1 = $vrm[1] .' ==> $v0 CHR ';
  if( is_numeric($vrm[1]) ):
    $result =  $vrm[0];
  else:
    $result = $vrm[0] .$vrm[1] ;             
  endif;   

  return $result;
}//


//=========================================
// Age identifier
//=========================================
function getV2
(
  string $vrm,
  string $v1
)
:string
{
# GET SECOND TWO CHARACTER
  $result = substr($vrm, strlen($v1), 2);

# IS $result NUMERIC
  $result = is_numeric($result) ? $result : '';

 return $result;
}//


//=========================================
// Random characters - min/max lengths
//=========================================
function getV3
(
  string $vrm, $first
)
: string
{
  $result = substr($vrm, strlen($first) );

  if( ctype_alpha($result) ) :
     // $result
  else:
     $result = '';
  endif;
         
  return $result;
}//


As previously mentioned I find this far easier to quickly understand at a cursory glance than trying to fathom out how the REGEX works :)
« Last Edit: 11 Jan 2021, 12:55:06 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

mmerlinn

  • Jr. Member
  • **
  • Posts: 77
  • Karma: +9/-0
  • Nutcake
Re: Messing? PHP Code but it works...
« Reply #24 on: 11 Jan 2021, 01:34:59 am »
@mmerlinn,
FoxPro !!!!! Reminds me of my Clipper days :)

I assumed that the minimum length of registration was six characters?

Following are the three PHP function to return "Area code", "Age identifier" and "Random letters". If the extraction fails then an "empty string" is returned. Only one function return value needs to be empty and the whole registration plate is invalid.

As previously mentioned I find this far easier to quickly understand at a cursory glance than trying to fathom out how the REGEX works :)

Yep, FoxPro 2.6 on a Mac. Too old (well past 70) to even consider moving to a newer platform as I would never live long enough to recover the learning curve.

PHP code similar to my FP code will not work for you without changes, but it will work for Grumpy based on his Regex notes.

Regex turns my brain to mush and since I don't need it, I avoid it.

I come from programming in Applesoft on a 16k machine. Everything there had to be compact and minimalist. That has carried through to where everything I program today is compact and minimalist as the above code demonstrates. After looking at it I see where I can reduce the code by another few bytes. I would have a hard time using three functions, like you do, to do what I think could be done with half the code in one function and still be readable.

I once typed in a sort program in Applesoft for my Apple II, all 250 lines of it. Took way too much memory and was slow (12 MINUTES to sort 2000 random 4-byte strings). After tinkering with the code for a few weeks, I finally settled on FIVE (5) lines of Applesoft code as being the fastest while a 3-line sort was slightly slower.  That code sorted 2000 random 4-byte strings in 12 SECONDS, many orders faster than the original 250 lines of code.  Further, I could sort ascending or descending while the original would only sort ascending.
The soul purr pus of a spell cheque cur is two valley date hour ignore ants.

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Messing? PHP Code but it works...
« Reply #25 on: 11 Jan 2021, 01:42:43 am »
The UK minimum length is 2 characters  - one letter and number!

Thank you for the assistance!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

mmerlinn

  • Jr. Member
  • **
  • Posts: 77
  • Karma: +9/-0
  • Nutcake
Re: Messing? PHP Code but it works...
« Reply #26 on: 11 Jan 2021, 02:25:47 am »
The UK minimum length is 2 characters  - one letter and number!

Thank you for the assistance!

That means a PHP version of the code I posted will work for you and you needn't fry your brain using Regex.
The soul purr pus of a spell cheque cur is two valley date hour ignore ants.

John_Betong

  • Full Member
  • ***
  • Posts: 218
  • Karma: +24/-1
    • The Fastest Joke Site On The Web
Re: Messing? PHP Code but it works...
« Reply #27 on: 11 Jan 2021, 02:27:57 am »
Single digit for the Age identifier? Not according to the supplied link?
Quote
A two-digit age identifier, which changes twice a year, in March and September. The code is either the last two digits of the year itself if issued between March and August (e.g. "10" for registrations issued between 1 March and 31 August 2010), or else has 50 added to that value if issued between September and February the following year (e.g. "60" for registrations issued between 1 September 2010 and 28 February 2011); 


I have amended then empty function getV3(...) test and it now allows an empty string and show a couple more valid registrations.
I look forward to seeing the final application.



[off-topic] Just noticed that the online solution is under "codingforum" instead of "cutcodedown" :) I must have been tired yesterday :) [/off-topic]
« Last Edit: 11 Jan 2021, 02:31:19 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

John_Betong

  • Full Member
  • ***
  • Posts: 218
  • Karma: +24/-1
    • The Fastest Joke Site On The Web
Re: Messing? PHP Code but it works...
« Reply #28 on: 11 Jan 2021, 02:45:03 am »
@merlinn,
Quote
I come from programming in Applesoft on a 16k machine. Everything there had to be compact and minimalist. That has carried through to where everything I program today is compact and minimalist as the above code demonstrates. After looking at it I see where I can reduce the code by another few bytes. I would have a hard time using three functions, like you do, to do what I think could be done with half the code in one function and still be readable.

My first personal computer was a 4k Tandy TRS80 with a tape-deck and saving bytes was mandatory to prevent out of memory errors.  Now I am more concerned with making my scripts easily readable because more time is spent trying to debug old code.

I am delighted that computers are so much faster and less expensive nowadays :)
« Last Edit: 11 Jan 2021, 02:49:27 am by John_Betong »
Retired in the City of Angels where the weather suits my clothes

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 787
  • Karma: +8/-0
    • Grumpy Young Man
Re: Messing? PHP Code but it works...
« Reply #29 on: 11 Jan 2021, 03:06:23 am »
Single digit for the Age identifier? Not according to the supplied link?
Quote
A two-digit age identifier, which changes twice a year, in March and September. The code is either the last two digits of the year itself if issued between March and August (e.g. "10" for registrations issued between 1 March and 31 August 2010), or else has 50 added to that value if issued between September and February the following year (e.g. "60" for registrations issued between 1 September 2010 and 28 February 2011); 


I have amended then empty function getV3(...) test and it now allows an empty string and show a couple more valid registrations.
I look forward to seeing the final application.



[off-topic] Just noticed that the online solution is under "codingforum" instead of "cutcodedown" :) I must have been tired yesterday :) [/off-topic]
It is still a valid registration... its just a personalised number plate and not an age identifiable registration! :)

Same as on styles they don't use I's Q's or Z's...

I've been in the motor trade since 1994....   :-X :o ::)

Thanks again for you all help merlinn and John!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

 

SMF spam blocked by CleanTalk

Advertisement