Solved

preg_replace with dash

Posted on 2011-03-04
8
881 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

726 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