Solved

convert to lowercase & change spaces to hyphen

Posted on 2011-09-23
6
590 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
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.

 
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

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

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

21 Experts available now in Live!

Get 1:1 Help Now