Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 06-08-2009, 02:58 PM PHP Regex
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
As of the writing of this post I'm programing a CMS. I'm including a textbox that allows the user to type in what they would like the 'slug' or name of the page to be. Everything works hunky-dory, provided that the user understands the unaccepted symbols and format for page titles.

I'm working on a regex (using the preg_match function, of course) that converts any thing incompatible into something that is compatible. So for instance, it would convert HEY O to hey-o.php, or hey & O to hey-o.php The problem is, I'm terrible with the regex stuff. No matter how hard I try to understand it, it all goes right over my head.

Can any of you help me out with some kind of regex that converts incompatible results to compatible ones?

Thanks!
__________________
if($stevej == "helpful") { $talkupation += $user_power; }
stevej is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-08-2009, 08:23 PM Re: PHP Regex
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
PHP Code:
// Get user input
$page $_POST['page'];
// Strip out unwanted chars and change case
$page preg_replace('/[^a-z0-9- ]/i'' 'strtolower($page));
// Remove multiple spaces
while (strpos($page'  ') !== false$page str_replace('  '' '$page);
// Convert spaces to dashes
$page str_replace(' ''-'trim($page));
// Check if string is valid
if (strlen($page) > 0)
{
  
// Page name is valid
  
$page $page '.php';
}
else
{
  
// Page name is not valid
  
$page '';

__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-08-2009, 09:05 PM Re: PHP Regex
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
No need for reg ex (though it was an excellent answer)
PHP Code:
$var "HEY O";
$filename urlencode(strtolower(str_replace(" ""-"trim($var)))).".php";
//ouputs "hey-o.php", will encode any string so it may be placed into a browser 
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 06-08-2009 at 09:13 PM.. Reason: added trim() to the innermost nest
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 06-09-2009, 09:52 PM Re: PHP Regex
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Wow! Thanks for the response guys. I honestly didn't think I'd get so much feedback. I found both snippets of code quite helpful, though I am afraid that wayfarer's didn't work... the main problem being that the urlencode() function replaces some symbols with weird stuff that's technically 'browser friendly' but doesn't really look good. So right now, I'm using a variation of mgraphic's code. I'd like to get your input on it.

PHP Code:
$page preg_replace('/[^a-z0-9-\. ]/i'''strtolower($page));
$page str_replace("  "" "$page);
$page str_replace(' ''-'trim($page));

if(!
preg_match('/\.php$/'$page)) {
    
$page $page.".php";

And mgraphic, I'm somewhat curious to know why you used strpos() for replacing the double-spaces with single spaces...

Thanks again!
__________________
if($stevej == "helpful") { $talkupation += $user_power; }
stevej is offline
Reply With Quote
View Public Profile
 
Old 06-10-2009, 06:01 AM Re: PHP Regex
Junior Talker

Posts: 1
Trades: 0
Great PHP POSIX Regexp explains exactly what you are looking for!
__________________
AlphaPatrol
network monitoring

Please login or register to view this content. Registration is FREE
alphapatrol is offline
Reply With Quote
View Public Profile
 
Old 06-10-2009, 11:08 AM Re: PHP Regex
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
page = preg_replace('/[^a-z0-9-\. ]/i', '', strtolower($page));
I don't think you need to escape a period in a characture class and also since you are forcing the string to lower case, you don't need the "case insensitive" modifier "i" (even though I did the same thing)

Quote:
$page = str_replace(" ", " ", $page);
I used a while() loop in my example to eleminate all double spaces. In your example above would change 3 spaces to 2 spaces and you still have a double space. I also use the strpos() because it will return an integer if it exists.

while (strpos($page, ' ') !== false) $page = str_replace(' ', ' ', $page);
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-10-2009, 04:46 PM Re: PHP Regex
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
Okay, cool. I though I needed to escape the period, but I tested it without and it works just fine. I also got rid of the unnecessary modifier at the end of the regex as well as well. Hooray, two less characters!

Thanks for the explanation for the whole while(strpos()) thing. Makes sense now. I also reversed the order of last bit, removing multiple spaces. Now it removes multiple hyphens, which prevents converting something like hey o- mate into hey-o--mate.php. So here's what I've got now.

PHP Code:
$page preg_replace('/[^a-z0-9-. ]/'''strtolower($page));
$page str_replace(' ''-'trim($page));
while (
strpos($page'--') !== false$page str_replace('--''-'$page);
if(!
preg_match('/\.php$/'$page)) {
    
$page $page.".php";

Thanks for all the help!
__________________
if($stevej == "helpful") { $talkupation += $user_power; }

Last edited by stevej; 06-10-2009 at 04:47 PM..
stevej is offline
Reply With Quote
View Public Profile
 
Old 06-10-2009, 09:18 PM Re: PHP Regex
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by stevej View Post
I also reversed the order of last bit, removing multiple spaces. Now it removes multiple hyphens, which prevents converting something like hey o- mate into hey-o--mate.php. So here's what I've got now.
Thats actually a better idea!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP Regex
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41865 seconds with 12 queries