?
Solved

preg_replace with dash

Posted on 2011-03-04
8
Medium Priority
?
902 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 1000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 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 1000 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 1000 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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Suggested Courses

777 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