The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 02:38:01 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: why is there no function that does....  (Read 2649 times)
ksan
Rookie
**
Offline Offline

Posts: 10


View Profile
« on: December 03, 2008, 11:20:51 AM »

Ok. So preg_match(), preg_replace() functions are really cool and you can do a lot of things with them. Filtering out the stuff that matches your criteria or replacing alle the other crap with something different, rocks.
However, wouldn't it be kinda handy to have something like preg_create() ?
A function that would create random strings that match the specified criteria. Whether you want to create lots of records for testing and benchmarking your database, or for submitting forms, something like that could be very useful.

I suppose in order to write such a function one would need to emulate the regex engine right ?
How hard would that be? Can you point me in the right direction ?
Of course, it'd be great if somebody around here already has something along those lines and was willing to share it with everyone?

Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: December 03, 2008, 11:31:40 AM »

An interesting notion, to a point. Specifically if you are posting data to forms, I think that the rules you might want to apply are far more complicated than what a simple Regex engine could accomplish. Consider, simply, that you'd like to randomize for 50 different first names and 50 different last names. Now I have no idea why you'd want to do such a thing...  ROFLMAO ROFLMAO ROFLMAO but that sort of action is much better coded directly into your Lingo du'Jour.

But all that said, if you really wanted to, yes - I think you'd need to emulate the RULES of the regex engine, but I don't think that anything that exists in the existing engines would actually help you out, because the action is so very different. I think you'd spend more time trying to work through the code and see what to change (and not get anywhere) than actually make progress.

IMO you'd be better off to get a book like Regular Expressions: The Complete Tutorial (Jan Goyvaerts), outline the rules that you wish to emulate, then add to them as you see fit and then create your own language essentially. I have done this several times in other scenarios (I've even officially released 3 languages for very specific purposes) and it's not trivial, but but is hugely rewarding when you get what you want. You may, in fact, find that as you work through what you want to accomplish, you really need a new language structure that REALLY gives you what you are looking for. This is probably the little-known reason for all contemporary programming/scripting languages.

If you really want to go down this path, then I can assist you in the design phase.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #2 on: December 03, 2008, 12:12:54 PM »

hmm...

Off the top of my head, I can't think of how, but I would be incredibly surprised if you could not do what you're looking for with just sed and awk.
Logged

hai
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #3 on: December 03, 2008, 12:14:24 PM »

Bah! I'm an idiot.

Code:
<?php
$generatedString 
shell_exec("cat /dev/urandom | grep -P 'YOUR_PCRE_REGEX_HERE_NO_DELIMITERS'");
?>

Logged

hai
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #4 on: December 03, 2008, 01:09:05 PM »

why do i get the feeling that all of V's code looks like
$something = shell_exec();
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #5 on: December 03, 2008, 01:21:07 PM »

Very little of it does, actually.

Best tool for the job man. Also I subscribe to the hacker ethos of "never reinvent the wheel". If someone's already written something that does it just fine, use their code, improve on their code, etc.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #6 on: December 03, 2008, 01:35:28 PM »

why do i get the feeling that all of V's code looks like

ROFLMAO
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!