Solved

preg_replace with dash

Posted on 2011-03-04
8
870 Views
Last Modified: 2012-05-11
Hi,

Just a quick question. I'm wondering if this code:
     function create_slug($string){  
    $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);  
    return $slug;  
  }  

Open in new window


will replace all instances of any character that is a letter or number with the dash. It's supposed to create user friendly post slugs from title string to use within URLs.

Thanks,
Victor
0
Comment
Question by:Victor Kimura
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

by:Vimal DM
ID: 35034747
Hai,

Just a slight modification there in your function

  function create_slug($string){  
    $slug=preg_replace('/[^A-Za-z]+/', '-', $string);
      
    return $slug;  
  }  
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 35035140
The pattern you posted '/[^A-Za-z0-9-]+/' will replace all characters that are NOT 0-9, A-Z, a-z or a dash

0
 

Author Comment

by:Victor Kimura
ID: 35035457
Hi bportlock,

Where is the NOT sign?

Thanks,
Victor
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 35035584
[^ ... ]

Open in new window


Means any character not in this character set. Remove the chevron to make it any character in this set.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 35035607
P.S.

The chevron only makes it a "not" when it is listed as the first character in the character set. Listed anywhere else, it is a literal character.

Examples:

[^abc]  - Any character NOT a, b, or c
[abc]   - a, b, or c
[abc^]  - a, b, c, or chevron
[^abc^] - Any character NOT a, b, c, or chevron
[^^]    - Any character NOT a chevron

Open in new window

0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 250 total points
ID: 35036525
"....Means any character not in this character set. Remove the chevron to make it any character in this set....."

Why didn't they use ! to mean NOT like many programming languages..... oh well

@vkimura2007 - I can understand your confusion look at this

 /^[^a-z]+[a-z]*$/

That means "any string starting (^) with characters that are NOT a-z ([^a-z]) ..... " etc. It's unfortunate that regex syntax looks like the leftovers from the Scrabble box....
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35036836
>>  ...regex syntax looks like the leftovers from the Scrabble box...

lol   = )
0
 

Author Closing Comment

by:Victor Kimura
ID: 35040801
Thanks, fellas. I totally agree. They should have simplified the regex patterns and be consistent with the other programming languages. =)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question