?
Solved

convert to lowercase & change spaces to hyphen

Posted on 2011-09-23
6
Medium Priority
?
626 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
[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
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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
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

Independent Software Vendors: 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

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‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 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