Making string contents short and simple

Posted on 2005-03-14
Medium Priority
Last Modified: 2006-11-17

How can I get rid of all non alphanumeric characters in a string, AND cut the string so that it is no longer than 10 characters?

So that:
   "123... This is a test & only a test!"


Question by:hankknight
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
LVL 10

Assisted Solution

eeBlueShadow earned 444 total points
ID: 13536100
$str = "123... This is a test & only a test!"

$str = preg_replace('|\W|', '', $str);
$str = substr($str, 0, 10);
LVL 32

Accepted Solution

Batalf earned 888 total points
ID: 13536104
$string = "123... This is a test & only a test!";
$string = substr(preg_replace("/[^a-zA-Z0-9]/si","",$string),0,10);

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

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…
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…
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…
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 …
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