Solved

convert to lowercase & change spaces to hyphen

Posted on 2011-09-23
6
584 Views
Last Modified: 2012-06-27
How do I convert the below to lowercase and change all spaces to a hyphen?

$row_rs_wp_posts['post_title']
0
Comment
Question by:petewinter
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
dsmile earned 375 total points
ID: 36585992

$row_rs_wp_posts['post_title'] = str_replace(' ', '-', strtolower($row_rs_wp_posts['post_title']));

Open in new window

0
 
LVL 13

Assisted Solution

by:dsmile
dsmile earned 375 total points
ID: 36586001
strtolower() changes string to lower case.
str_replace() replaces first param by second param
0
 

Author Comment

by:petewinter
ID: 36586047
Thanks. Sorry I also need it to remove any special characters like & ,etc. How can I do this?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 13

Assisted Solution

by:dsmile
dsmile earned 375 total points
ID: 36586100
Simple approach: add characters you want to replace in $arySearch, and '-' to $aryReplace (number of elements must be equal)


$row_rs_wp_posts['post_title'] = strtolower($row_rs_wp_posts['post_title']); //convert string to lower
$arySearch = array(' ', '&');
$aryReplace = array('-', '-');
$row_rs_wp_posts['post_title'] = str_replace($arySearch, $aryReplace, $row_rs_wp_posts['post_title']); //replace characters

Open in new window


Advanced way: read about preg_replace and use it for replacing work
0
 
LVL 3

Assisted Solution

by:FrankoH
FrankoH earned 125 total points
ID: 36586106
here is a small function i use to clean up urls, it should do the trick, just remove/add characters to suit.

function UrlSafeString( $strVal ) {
	
		// Replaces illegal url characters with a safe alternative
			
		$strSearch = array( '"',"'", "?", " ", "/", "\\", "@", "=", "|", "+", "(", ")", "[", "]", "{", "}", "*",   "&", "#", "!", "." );
		$strReplace = array("" , "",  "", "-", "-",  "-", "-", "-", "-", "-",  "",  "",  "",  "",  "",  "",  "",  "and",  "",  "",  "" );
	
		return str_replace($strSearch, $strReplace, strtolower($strVal) );
		
		}

Open in new window

0
 

Author Closing Comment

by:petewinter
ID: 36586183
Many thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

758 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

23 Experts available now in Live!

Get 1:1 Help Now