Solved

preg_replace with dash

Posted on 2011-03-04
8
864 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
 
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
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now