?
Solved

convert to lowercase & change spaces to hyphen

Posted on 2011-09-23
6
Medium Priority
?
643 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 1500 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 1500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Assisted Solution

by:dsmile
dsmile earned 1500 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
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.
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 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